Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
64b85438
Commit
64b85438
authored
Dec 23, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding id accessor
parent
287d2c7e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
108 additions
and
69 deletions
+108
-69
phpunit.xml
phpunit.xml
+2
-0
Model.php
src/Jenssegers/Mongodb/Model.php
+22
-2
ModelTest.php
tests/ModelTest.php
+9
-0
MysqlRelationsTest.php
tests/MysqlRelationsTest.php
+75
-0
RelationsTest.php
tests/RelationsTest.php
+0
-67
No files found.
phpunit.xml
View file @
64b85438
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
<testsuites>
<testsuites>
<testsuite
name=
"all"
>
<testsuite
name=
"all"
>
<directory>
tests/
</directory>
<directory>
tests/
</directory>
<exclude>
tests/MysqlRelationsTest.php
</exclude>
</testsuite>
</testsuite>
<testsuite
name=
"schema"
>
<testsuite
name=
"schema"
>
<directory>
tests/SchemaTest.php
</directory>
<directory>
tests/SchemaTest.php
</directory>
...
@@ -33,6 +34,7 @@
...
@@ -33,6 +34,7 @@
</testsuite>
</testsuite>
<testsuite
name=
"relations"
>
<testsuite
name=
"relations"
>
<directory>
tests/RelationsTest.php
</directory>
<directory>
tests/RelationsTest.php
</directory>
<directory>
tests/MysqlRelationsTest.php
</directory>
</testsuite>
</testsuite>
</testsuites>
</testsuites>
</phpunit>
</phpunit>
src/Jenssegers/Mongodb/Model.php
View file @
64b85438
...
@@ -37,6 +37,19 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -37,6 +37,19 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
*/
protected
static
$resolver
;
protected
static
$resolver
;
/**
* Custom accessor for the model's id.
*
* @return string
*/
public
function
getIdAttribute
(
$value
)
{
// If there is an actual id attribute, then return that.
if
(
$value
)
return
$value
;
return
$this
->
getKey
();
}
/**
/**
* Convert a DateTime to a storable MongoDate object.
* Convert a DateTime to a storable MongoDate object.
*
*
...
@@ -132,7 +145,10 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -132,7 +145,10 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
{
{
foreach
(
$attributes
as
$key
=>
&
$value
)
foreach
(
$attributes
as
$key
=>
&
$value
)
{
{
// Convert MongoId to string
/**
* MongoIds are converted to string to make it easier to pass
* the id to other instances or relations.
*/
if
(
$value
instanceof
MongoId
)
if
(
$value
instanceof
MongoId
)
{
{
$value
=
(
string
)
$value
;
$value
=
(
string
)
$value
;
...
@@ -153,7 +169,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -153,7 +169,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
foreach
(
$attributes
as
&
$value
)
foreach
(
$attributes
as
&
$value
)
{
{
// Convert MongoDate to string
/**
* Here we convert MongoDate instances to string. This mimics
* original SQL behaviour so that dates are formatted nicely
* when your models are converted to JSON.
*/
if
(
$value
instanceof
MongoDate
)
if
(
$value
instanceof
MongoDate
)
{
{
$value
=
$this
->
asDateTime
(
$value
)
->
format
(
'Y-m-d H:i:s'
);
$value
=
$this
->
asDateTime
(
$value
)
->
format
(
'Y-m-d H:i:s'
);
...
...
tests/ModelTest.php
View file @
64b85438
...
@@ -334,4 +334,13 @@ class ModelTest extends PHPUnit_Framework_TestCase {
...
@@ -334,4 +334,13 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
}
}
public
function
testIdAttribute
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$this
->
assertEquals
(
$user
->
id
,
$user
->
_id
);
$user
=
User
::
create
(
array
(
'id'
=>
'custom_id'
,
'name'
=>
'John Doe'
));
$this
->
assertNotEquals
(
$user
->
id
,
$user
->
_id
);
}
}
}
tests/MysqlRelationsTest.php
0 → 100644
View file @
64b85438
<?php
class
MysqlRelationsTest
extends
PHPUnit_Framework_TestCase
{
public
function
setUp
()
{
MysqlUser
::
executeSchema
();
MysqlBook
::
executeSchema
();
MysqlRole
::
executeSchema
();
}
public
function
tearDown
()
{
MysqlUser
::
truncate
();
MysqlBook
::
truncate
();
MysqlRole
::
truncate
();
}
public
function
testMysqlRelations
()
{
$user
=
new
MysqlUser
;
$this
->
assertInstanceOf
(
'MysqlUser'
,
$user
);
$this
->
assertInstanceOf
(
'Illuminate\Database\MySqlConnection'
,
$user
->
getConnection
());
// Mysql User
$user
->
name
=
"John Doe"
;
$user
->
save
();
$this
->
assertTrue
(
is_int
(
$user
->
id
));
// SQL has many
$book
=
new
Book
(
array
(
'title'
=>
'Game of Thrones'
));
$user
->
books
()
->
save
(
$book
);
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$this
->
assertEquals
(
1
,
count
(
$user
->
books
));
// MongoDB belongs to
$book
=
$user
->
books
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$book
->
mysqlAuthor
->
name
);
// SQL has one
$role
=
new
Role
(
array
(
'type'
=>
'admin'
));
$user
->
role
()
->
save
(
$role
);
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$this
->
assertEquals
(
'admin'
,
$user
->
role
->
type
);
// MongoDB beelongs to
$role
=
$user
->
role
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$role
->
mysqlUser
->
name
);
// MongoDB User
$user
=
new
User
;
$user
->
name
=
"John Doe"
;
$user
->
save
();
// MongoDB has many
$book
=
new
MysqlBook
(
array
(
'title'
=>
'Game of Thrones'
));
$user
->
mysqlBooks
()
->
save
(
$book
);
$user
=
User
::
find
(
$user
->
_id
);
// refetch
$this
->
assertEquals
(
1
,
count
(
$user
->
mysqlBooks
));
// SQL belongs to
$book
=
$user
->
mysqlBooks
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$book
->
author
->
name
);
// MongoDB has one
$role
=
new
MysqlRole
(
array
(
'type'
=>
'admin'
));
$user
->
mysqlRole
()
->
save
(
$role
);
$user
=
User
::
find
(
$user
->
_id
);
// refetch
$this
->
assertEquals
(
'admin'
,
$user
->
mysqlRole
->
type
);
// SQL belongs to
$role
=
$user
->
mysqlRole
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$role
->
user
->
name
);
}
}
tests/RelationsTest.php
View file @
64b85438
...
@@ -259,71 +259,4 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -259,71 +259,4 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
$group
->
_id
,
$user
->
groups
()
->
first
()
->
_id
);
$this
->
assertEquals
(
$group
->
_id
,
$user
->
groups
()
->
first
()
->
_id
);
$this
->
assertEquals
(
$user
->
_id
,
$group
->
users
()
->
first
()
->
_id
);
$this
->
assertEquals
(
$user
->
_id
,
$group
->
users
()
->
first
()
->
_id
);
}
}
public
function
testMysqlModel
()
{
// A bit dirty
MysqlUser
::
executeSchema
();
MysqlBook
::
executeSchema
();
MysqlRole
::
executeSchema
();
$user
=
new
MysqlUser
;
$this
->
assertInstanceOf
(
'MysqlUser'
,
$user
);
$this
->
assertInstanceOf
(
'Illuminate\Database\MySqlConnection'
,
$user
->
getConnection
());
// Mysql User
$user
->
name
=
"John Doe"
;
$user
->
save
();
$this
->
assertTrue
(
is_int
(
$user
->
id
));
// SQL has many
$book
=
new
Book
(
array
(
'title'
=>
'Game of Thrones'
));
$user
->
books
()
->
save
(
$book
);
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$this
->
assertEquals
(
1
,
count
(
$user
->
books
));
// MongoDB belongs to
$book
=
$user
->
books
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$book
->
mysqlAuthor
->
name
);
// SQL has one
$role
=
new
Role
(
array
(
'type'
=>
'admin'
));
$user
->
role
()
->
save
(
$role
);
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$this
->
assertEquals
(
'admin'
,
$user
->
role
->
type
);
// MongoDB beelongs to
$role
=
$user
->
role
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$role
->
mysqlUser
->
name
);
// MongoDB User
$user
=
new
User
;
$user
->
name
=
"John Doe"
;
$user
->
save
();
// MongoDB has many
$book
=
new
MysqlBook
(
array
(
'title'
=>
'Game of Thrones'
));
$user
->
mysqlBooks
()
->
save
(
$book
);
$user
=
User
::
find
(
$user
->
_id
);
// refetch
$this
->
assertEquals
(
1
,
count
(
$user
->
mysqlBooks
));
// SQL belongs to
$book
=
$user
->
mysqlBooks
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$book
->
author
->
name
);
// MongoDB has one
$role
=
new
MysqlRole
(
array
(
'type'
=>
'admin'
));
$user
->
mysqlRole
()
->
save
(
$role
);
$user
=
User
::
find
(
$user
->
_id
);
// refetch
$this
->
assertEquals
(
'admin'
,
$user
->
mysqlRole
->
type
);
// SQL belongs to
$role
=
$user
->
mysqlRole
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$role
->
user
->
name
);
// Dirty again :)
MysqlUser
::
truncate
();
MysqlBook
::
truncate
();
MysqlRole
::
truncate
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment