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
0eed8964
Commit
0eed8964
authored
Nov 20, 2013
by
unknown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Models fixed and tests updated
parent
bbb79909
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
192 additions
and
145 deletions
+192
-145
Model.php
src/Jenssegers/Mongodb/Model.php
+62
-38
ModelTest.php
tests/ModelTest.php
+3
-0
RelationsTest.php
tests/RelationsTest.php
+124
-105
User.php
tests/models/User.php
+3
-2
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
0eed8964
...
...
@@ -9,6 +9,7 @@ use Jenssegers\Mongodb\Builder as QueryBuilder;
use
Jenssegers\Mongodb\Relations\BelongsTo
;
use
Jenssegers\Mongodb\Relations\BelongsToMany
;
use
Carbon\Carbon
;
use
DateTime
;
use
MongoId
;
use
MongoDate
;
...
...
@@ -67,19 +68,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
protected
function
asDateTime
(
$value
)
{
// Convert MongoDate to timestamp
if
(
$value
instanceof
MongoDate
)
// Convert timestamp
if
(
is_numeric
(
$value
))
{
return
Carbon
::
createFromTimestamp
(
$value
);
}
// Convert string
if
(
is_string
(
$value
))
{
$value
=
$value
->
sec
;
return
new
Carbon
(
$value
)
;
}
// Convert
timestamp to string for DateTim
e
if
(
is_int
(
$value
)
)
// Convert
MongoDat
e
if
(
$value
instanceof
MongoDate
)
{
$value
=
"@
$value
"
;
return
Carbon
::
createFromTimestamp
(
$value
->
sec
)
;
}
return
new
DateTim
e
(
$value
);
return
Carbon
::
instanc
e
(
$value
);
}
/**
...
...
@@ -115,68 +122,84 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
}
/**
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public
function
hasOne
(
$related
,
$foreignKey
=
null
)
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public
function
hasOne
(
$related
,
$foreignKey
=
null
,
$localKey
=
null
)
{
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$instance
=
new
$related
;
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasOne
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
/**
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public
function
hasMany
(
$related
,
$foreignKey
=
null
)
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public
function
hasMany
(
$related
,
$foreignKey
=
null
,
$localKey
=
null
)
{
$foreignKey
=
$foreignKey
?:
$this
->
getForeignKey
();
$instance
=
new
$related
;
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
);
$localKey
=
$localKey
?:
$this
->
getKeyName
();
return
new
HasMany
(
$instance
->
newQuery
(),
$this
,
$foreignKey
,
$localKey
);
}
/**
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public
function
belongsTo
(
$related
,
$foreignKey
=
null
)
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public
function
belongsTo
(
$related
,
$foreignKey
=
null
,
$otherKey
=
null
,
$relation
=
null
)
{
list
(,
$caller
)
=
debug_backtrace
(
false
);
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if
(
is_null
(
$relation
))
{
list
(,
$caller
)
=
debug_backtrace
(
false
);
$relation
=
$caller
[
'function'
];
}
// If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
$relation
=
$caller
[
'function'
];
if
(
is_null
(
$foreignKey
))
{
$foreignKey
=
snake_case
(
$relation
)
.
'_id'
;
}
$instance
=
new
$related
;
// Once we have the foreign key names, we'll just create a new Eloquent query
// for the related models and returns the relationship instance which will
// actually be responsible for retrieving and hydrating every relations.
$instance
=
new
$related
;
$query
=
$instance
->
newQuery
();
return
new
BelongsTo
(
$query
,
$this
,
$foreignKey
,
$relation
);
$otherKey
=
$otherKey
?:
$instance
->
getKeyName
();
return
new
BelongsTo
(
$query
,
$this
,
$foreignKey
,
$otherKey
,
$relation
);
}
/**
* Define a many-to-many relationship.
*
...
...
@@ -266,7 +289,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
{
$this
->
__unset
(
$column
);
}
// Perform unset only on current document
return
$query
=
$this
->
newQuery
()
->
where
(
$this
->
getKeyName
(),
$this
->
getKey
())
->
unset
(
$columns
);
}
...
...
@@ -280,6 +303,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
public
function
__call
(
$method
,
$parameters
)
{
// Unset method
if
(
$method
==
'unset'
)
{
return
call_user_func_array
(
array
(
$this
,
'dropColumn'
),
$parameters
);
...
...
tests/ModelTest.php
View file @
0eed8964
...
...
@@ -312,11 +312,14 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public
function
testDates
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'birthday'
=>
new
DateTime
(
'1980/1/1'
)));
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$user
->
birthday
);
$check
=
User
::
find
(
$user
->
_id
);
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$check
->
birthday
);
$this
->
assertEquals
(
$user
->
birthday
,
$check
->
birthday
);
$user
=
User
::
where
(
'birthday'
,
'>'
,
new
DateTime
(
'1975/1/1'
))
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
...
...
tests/RelationsTest.php
View file @
0eed8964
...
...
@@ -3,110 +3,128 @@
class
RelationsTest
extends
PHPUnit_Framework_TestCase
{
public
function
setUp
()
{
User
::
truncate
();
Book
::
truncate
();
Item
::
truncate
();
Role
::
truncate
();
Client
::
truncate
();
}
public
function
tearDown
()
{
User
::
truncate
();
Book
::
truncate
();
Item
::
truncate
();
Role
::
truncate
();
Client
::
truncate
();
}
public
function
testHasMany
()
{
$author
=
User
::
create
(
array
(
'name'
=>
'George R. R. Martin'
));
Book
::
create
(
array
(
'title'
=>
'A Game of Thrones'
,
'author_id'
=>
$author
->
_id
));
Book
::
create
(
array
(
'title'
=>
'A Clash of Kings'
,
'author_id'
=>
$author
->
_id
));
$books
=
$author
->
books
;
$this
->
assertEquals
(
2
,
count
(
$books
));
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Item
::
create
(
array
(
'type'
=>
'knife'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'shield'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'bag'
,
'user_id'
=>
null
));
$items
=
$user
->
items
;
$this
->
assertEquals
(
3
,
count
(
$items
));
}
public
function
testBelongsTo
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'George R. R. Martin'
));
Book
::
create
(
array
(
'title'
=>
'A Game of Thrones'
,
'author_id'
=>
$user
->
_id
));
$book
=
Book
::
create
(
array
(
'title'
=>
'A Clash of Kings'
,
'author_id'
=>
$user
->
_id
));
$author
=
$book
->
author
;
$this
->
assertEquals
(
'George R. R. Martin'
,
$author
->
name
);
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$item
=
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
$owner
=
$item
->
user
;
$this
->
assertEquals
(
'John Doe'
,
$owner
->
name
);
}
public
function
testHasOne
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Role
::
create
(
array
(
'type'
=>
'admin'
,
'user_id'
=>
$user
->
_id
));
$role
=
$user
->
role
;
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
public
function
testWithBelongsTo
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Item
::
create
(
array
(
'type'
=>
'knife'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'shield'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'bag'
,
'user_id'
=>
null
));
$items
=
Item
::
with
(
'user'
)
->
get
();
$user
=
$items
[
0
]
->
getRelation
(
'user'
);
$this
->
assertInstanceOf
(
'User'
,
$user
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
1
,
count
(
$items
[
0
]
->
getRelations
()));
$this
->
assertEquals
(
null
,
$items
[
3
]
->
getRelation
(
'user'
));
}
public
function
testWithHashMany
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Item
::
create
(
array
(
'type'
=>
'knife'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'shield'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'bag'
,
'user_id'
=>
null
));
$user
=
User
::
with
(
'items'
)
->
find
(
$user
->
_id
);
$items
=
$user
->
getRelation
(
'items'
);
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertInstanceOf
(
'Item'
,
$items
[
0
]);
}
public
function
testWithHasOne
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Role
::
create
(
array
(
'type'
=>
'admin'
,
'user_id'
=>
$user
->
_id
));
Role
::
create
(
array
(
'type'
=>
'guest'
,
'user_id'
=>
$user
->
_id
));
$user
=
User
::
with
(
'role'
)
->
find
(
$user
->
_id
);
$role
=
$user
->
getRelation
(
'role'
);
$this
->
assertInstanceOf
(
'Role'
,
$role
);
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
}
public
function
tearDown
()
{
User
::
truncate
();
Book
::
truncate
();
Item
::
truncate
();
Role
::
truncate
();
Client
::
truncate
();
}
public
function
testHasMany
()
{
$author
=
User
::
create
(
array
(
'name'
=>
'George R. R. Martin'
));
Book
::
create
(
array
(
'title'
=>
'A Game of Thrones'
,
'author_id'
=>
$author
->
_id
));
Book
::
create
(
array
(
'title'
=>
'A Clash of Kings'
,
'author_id'
=>
$author
->
_id
));
$books
=
$author
->
books
;
$this
->
assertEquals
(
2
,
count
(
$books
));
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Item
::
create
(
array
(
'type'
=>
'knife'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'shield'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'bag'
,
'user_id'
=>
null
));
$items
=
$user
->
items
;
$this
->
assertEquals
(
3
,
count
(
$items
));
}
public
function
testBelongsTo
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'George R. R. Martin'
));
Book
::
create
(
array
(
'title'
=>
'A Game of Thrones'
,
'author_id'
=>
$user
->
_id
));
$book
=
Book
::
create
(
array
(
'title'
=>
'A Clash of Kings'
,
'author_id'
=>
$user
->
_id
));
$author
=
$book
->
author
;
$this
->
assertEquals
(
'George R. R. Martin'
,
$author
->
name
);
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$item
=
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
$owner
=
$item
->
user
;
$this
->
assertEquals
(
'John Doe'
,
$owner
->
name
);
}
public
function
testHasOne
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Role
::
create
(
array
(
'type'
=>
'admin'
,
'user_id'
=>
$user
->
_id
));
$role
=
$user
->
role
;
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
public
function
testWithBelongsTo
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Item
::
create
(
array
(
'type'
=>
'knife'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'shield'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'bag'
,
'user_id'
=>
null
));
$items
=
Item
::
with
(
'user'
)
->
get
();
$user
=
$items
[
0
]
->
getRelation
(
'user'
);
$this
->
assertInstanceOf
(
'User'
,
$user
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
1
,
count
(
$items
[
0
]
->
getRelations
()));
$this
->
assertEquals
(
null
,
$items
[
3
]
->
getRelation
(
'user'
));
}
public
function
testWithHashMany
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Item
::
create
(
array
(
'type'
=>
'knife'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'shield'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'sword'
,
'user_id'
=>
$user
->
_id
));
Item
::
create
(
array
(
'type'
=>
'bag'
,
'user_id'
=>
null
));
$user
=
User
::
with
(
'items'
)
->
find
(
$user
->
_id
);
$items
=
$user
->
getRelation
(
'items'
);
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertInstanceOf
(
'Item'
,
$items
[
0
]);
}
public
function
testWithHasOne
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
Role
::
create
(
array
(
'type'
=>
'admin'
,
'user_id'
=>
$user
->
_id
));
Role
::
create
(
array
(
'type'
=>
'guest'
,
'user_id'
=>
$user
->
_id
));
$user
=
User
::
with
(
'role'
)
->
find
(
$user
->
_id
);
$role
=
$user
->
getRelation
(
'role'
);
$this
->
assertInstanceOf
(
'Role'
,
$role
);
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
public
function
testEasyRelation
()
{
// Has Many
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$item
=
Item
::
create
(
array
(
'type'
=>
'knife'
));
$user
->
items
()
->
save
(
$item
);
$user
=
User
::
find
(
$user
->
_id
);
$items
=
$user
->
items
;
$this
->
assertEquals
(
1
,
count
(
$items
));
$this
->
assertInstanceOf
(
'Item'
,
$items
[
0
]);
// Has one
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$role
=
Role
::
create
(
array
(
'type'
=>
'admin'
));
$user
->
role
()
->
save
(
$role
);
$user
=
User
::
find
(
$user
->
_id
);
$role
=
$user
->
role
;
$this
->
assertInstanceOf
(
'Role'
,
$role
);
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
public
function
testHasManyAndBelongsTo
()
{
...
...
@@ -175,7 +193,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Client
::
create
(
array
(
'name'
=>
'Boloni Ltd.'
))
->
_id
,
Client
::
create
(
array
(
'name'
=>
'Meatballs Inc.'
))
->
_id
);
// Sync multiple records
$user
->
clients
()
->
sync
(
$clients
);
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
...
...
tests/models/User.php
View file @
0eed8964
...
...
@@ -8,7 +8,9 @@ use Illuminate\Auth\Reminders\RemindableInterface;
class
User
extends
Eloquent
implements
UserInterface
,
RemindableInterface
{
protected
$collection
=
'users'
;
protected
$dates
=
array
(
'birthday'
);
protected
static
$unguarded
=
true
;
public
function
books
()
...
...
@@ -60,5 +62,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
return
$this
->
email
;
}
}
\ No newline at end of file
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