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
a54ef6d9
Commit
a54ef6d9
authored
Mar 04, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into develop
parents
e647782f
9c8bf642
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
40 deletions
+56
-40
Model.php
src/Jenssegers/Mongodb/Model.php
+10
-10
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+3
-8
ModelTest.php
tests/ModelTest.php
+2
-2
QueryBuilderTest.php
tests/QueryBuilderTest.php
+13
-11
QueryTest.php
tests/QueryTest.php
+3
-3
RelationsTest.php
tests/RelationsTest.php
+25
-6
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
a54ef6d9
...
@@ -57,16 +57,6 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -57,16 +57,6 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
*/
protected
function
embedsMany
(
$related
,
$localKey
=
null
,
$foreignKey
=
null
,
$relation
=
null
)
protected
function
embedsMany
(
$related
,
$localKey
=
null
,
$foreignKey
=
null
,
$relation
=
null
)
{
{
if
(
is_null
(
$localKey
))
{
$localKey
=
snake_case
(
str_plural
(
$related
))
.
'_ids'
;
}
if
(
is_null
(
$foreignKey
))
{
$foreignKey
=
snake_case
(
class_basename
(
$this
));
}
// If no relation name was given, we will use this debug backtrace to extract
// 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
// 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.
// of the time this will be what we desire to use for the relatinoships.
...
@@ -77,6 +67,16 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -77,6 +67,16 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
$relation
=
$caller
[
'function'
];
$relation
=
$caller
[
'function'
];
}
}
if
(
is_null
(
$localKey
))
{
$localKey
=
'_'
.
$relation
;
}
if
(
is_null
(
$foreignKey
))
{
$foreignKey
=
snake_case
(
class_basename
(
$this
));
}
$query
=
$this
->
newQuery
();
$query
=
$this
->
newQuery
();
$instance
=
new
$related
;
$instance
=
new
$related
;
...
...
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
a54ef6d9
...
@@ -141,7 +141,7 @@ class EmbedsMany extends Relation {
...
@@ -141,7 +141,7 @@ class EmbedsMany extends Relation {
public
function
save
(
Model
$model
)
public
function
save
(
Model
$model
)
{
{
// Insert a new document.
// Insert a new document.
if
(
!
$model
->
exists
)
if
(
!
$model
->
exists
)
{
{
return
$this
->
performInsert
(
$model
);
return
$this
->
performInsert
(
$model
);
}
}
...
@@ -162,7 +162,7 @@ class EmbedsMany extends Relation {
...
@@ -162,7 +162,7 @@ class EmbedsMany extends Relation {
protected
function
performInsert
(
Model
$model
)
protected
function
performInsert
(
Model
$model
)
{
{
// Create a new key.
// Create a new key.
if
(
!
isset
(
$model
[
'_id'
]
))
if
(
!
$model
->
getAttribute
(
'_id'
))
{
{
$model
->
setAttribute
(
'_id'
,
new
MongoId
);
$model
->
setAttribute
(
'_id'
,
new
MongoId
);
}
}
...
@@ -265,12 +265,7 @@ class EmbedsMany extends Relation {
...
@@ -265,12 +265,7 @@ class EmbedsMany extends Relation {
*/
*/
public
function
createMany
(
array
$records
)
public
function
createMany
(
array
$records
)
{
{
$instances
=
array
();
$instances
=
array_map
(
array
(
$this
,
'create'
),
$records
);
foreach
(
$records
as
$record
)
{
$instances
[]
=
$this
->
create
(
$record
);
}
return
$instances
;
return
$instances
;
}
}
...
...
tests/ModelTest.php
View file @
a54ef6d9
...
@@ -104,8 +104,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
...
@@ -104,8 +104,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$all
=
User
::
all
();
$all
=
User
::
all
();
$this
->
assertEquals
(
2
,
count
(
$all
));
$this
->
assertEquals
(
2
,
count
(
$all
));
$this
->
assert
Equals
(
'John Doe'
,
$all
[
0
]
->
name
);
$this
->
assert
Contains
(
'John Doe'
,
$all
->
lists
(
'name'
)
);
$this
->
assert
Equals
(
'Jane Doe'
,
$all
[
1
]
->
name
);
$this
->
assert
Contains
(
'Jane Doe'
,
$all
->
lists
(
'name'
)
);
}
}
public
function
testFind
()
public
function
testFind
()
...
...
tests/QueryBuilderTest.php
View file @
a54ef6d9
...
@@ -78,10 +78,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
...
@@ -78,10 +78,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$users
=
DB
::
collection
(
'users'
)
->
get
();
$users
=
DB
::
collection
(
'users'
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$this
->
assertEquals
(
2
,
count
(
$users
));
$this
->
assertTrue
(
is_array
(
$users
[
0
][
'tags'
]));
$user
=
$users
[
0
];
$this
->
assertEquals
(
'Jane Doe'
,
$user
[
'name'
]);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
}
}
public
function
testFind
()
public
function
testFind
()
...
@@ -118,8 +115,10 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
...
@@ -118,8 +115,10 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'John Doe'
)
->
update
(
array
(
'age'
=>
100
));
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'John Doe'
)
->
update
(
array
(
'age'
=>
100
));
$users
=
DB
::
collection
(
'users'
)
->
get
();
$users
=
DB
::
collection
(
'users'
)
->
get
();
$this
->
assertEquals
(
20
,
$users
[
0
][
'age'
]);
$john
=
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
100
,
$users
[
1
][
'age'
]);
$jane
=
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'Jane Doe'
)
->
first
();
$this
->
assertEquals
(
100
,
$john
[
'age'
]);
$this
->
assertEquals
(
20
,
$jane
[
'age'
]);
}
}
public
function
testDelete
()
public
function
testDelete
()
...
@@ -312,9 +311,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
...
@@ -312,9 +311,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
));
));
$items
=
DB
::
collection
(
'items'
)
->
take
(
2
)
->
get
();
$items
=
DB
::
collection
(
'items'
)
->
orderBy
(
'name'
)
->
take
(
2
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertEquals
(
'
knife
'
,
$items
[
0
][
'name'
]);
$this
->
assertEquals
(
'
fork
'
,
$items
[
0
][
'name'
]);
}
}
public
function
testSkip
()
public
function
testSkip
()
...
@@ -326,7 +325,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
...
@@ -326,7 +325,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
));
));
$items
=
DB
::
collection
(
'items'
)
->
skip
(
2
)
->
get
();
$items
=
DB
::
collection
(
'items'
)
->
orderBy
(
'name'
)
->
skip
(
2
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertEquals
(
'spoon'
,
$items
[
0
][
'name'
]);
$this
->
assertEquals
(
'spoon'
,
$items
[
0
][
'name'
]);
}
}
...
@@ -352,8 +351,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
...
@@ -352,8 +351,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
));
));
$list
=
DB
::
collection
(
'items'
)
->
lists
(
'name'
);
$list
=
DB
::
collection
(
'items'
)
->
lists
(
'name'
);
sort
(
$list
);
$this
->
assertEquals
(
4
,
count
(
$list
));
$this
->
assertEquals
(
4
,
count
(
$list
));
$this
->
assertEquals
(
array
(
'
knife'
,
'fork
'
,
'spoon'
,
'spoon'
),
$list
);
$this
->
assertEquals
(
array
(
'
fork'
,
'knife
'
,
'spoon'
,
'spoon'
),
$list
);
$list
=
DB
::
collection
(
'items'
)
->
lists
(
'type'
,
'name'
);
$list
=
DB
::
collection
(
'items'
)
->
lists
(
'type'
,
'name'
);
$this
->
assertEquals
(
3
,
count
(
$list
));
$this
->
assertEquals
(
3
,
count
(
$list
));
...
@@ -470,7 +470,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
...
@@ -470,7 +470,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$results
=
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'exists'
,
true
)
->
get
();
$results
=
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'exists'
,
true
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$results
));
$this
->
assertEquals
(
2
,
count
(
$results
));
$this
->
assertEquals
(
'John Doe'
,
$results
[
0
][
'name'
]);
$resultsNames
=
array
(
$results
[
0
][
'name'
],
$results
[
1
][
'name'
]);
$this
->
assertContains
(
'John Doe'
,
$resultsNames
);
$this
->
assertContains
(
'Robert Roe'
,
$resultsNames
);
$results
=
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'exists'
,
false
)
->
get
();
$results
=
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'exists'
,
false
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$results
));
$this
->
assertEquals
(
1
,
count
(
$results
));
...
...
tests/QueryTest.php
View file @
a54ef6d9
...
@@ -70,18 +70,18 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -70,18 +70,18 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public
function
testSelect
()
public
function
testSelect
()
{
{
$user
=
User
::
select
(
'name'
)
->
first
();
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
select
(
'name'
)
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$user
=
User
::
select
(
'name'
,
'title'
)
->
first
();
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
select
(
'name'
,
'title'
)
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'admin'
,
$user
->
title
);
$this
->
assertEquals
(
'admin'
,
$user
->
title
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$user
=
User
::
get
(
array
(
'name'
))
->
first
();
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
get
(
array
(
'name'
))
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$this
->
assertEquals
(
null
,
$user
->
age
);
...
...
tests/RelationsTest.php
View file @
a54ef6d9
...
@@ -146,13 +146,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -146,13 +146,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertTrue
(
array_key_exists
(
'user_ids'
,
$client
->
getAttributes
()));
$this
->
assertTrue
(
array_key_exists
(
'user_ids'
,
$client
->
getAttributes
()));
$this
->
assertTrue
(
array_key_exists
(
'client_ids'
,
$user
->
getAttributes
()));
$this
->
assertTrue
(
array_key_exists
(
'client_ids'
,
$user
->
getAttributes
()));
$
client
s
=
$client
->
getRelation
(
'users'
);
$
user
s
=
$client
->
getRelation
(
'users'
);
$
user
s
=
$user
->
getRelation
(
'clients'
);
$
client
s
=
$user
->
getRelation
(
'clients'
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$users
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$users
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$clients
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$clients
);
$this
->
assertInstanceOf
(
'Client'
,
$
user
s
[
0
]);
$this
->
assertInstanceOf
(
'Client'
,
$
client
s
[
0
]);
$this
->
assertInstanceOf
(
'User'
,
$
client
s
[
0
]);
$this
->
assertInstanceOf
(
'User'
,
$
user
s
[
0
]);
$this
->
assertCount
(
2
,
$user
->
clients
);
$this
->
assertCount
(
2
,
$user
->
clients
);
$this
->
assertCount
(
1
,
$client
->
users
);
$this
->
assertCount
(
1
,
$client
->
users
);
...
@@ -289,6 +289,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -289,6 +289,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$address
=
new
Address
(
array
(
'city'
=>
'London'
));
$address
=
new
Address
(
array
(
'city'
=>
'London'
));
$address
=
$user
->
addresses
()
->
save
(
$address
);
$address
=
$user
->
addresses
()
->
save
(
$address
);
$this
->
assertNotNull
(
$user
->
_addresses
);
$this
->
assertEquals
(
array
(
'London'
),
$user
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'London'
),
$user
->
addresses
->
lists
(
'city'
));
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
created_at
);
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
created_at
);
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
updated_at
);
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
updated_at
);
...
@@ -340,12 +341,30 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -340,12 +341,30 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
public
function
testEmbedsManyCreate
()
public
function
testEmbedsManyCreate
()
{
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
=
User
::
create
(
array
());
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'Bruxelles'
));
$address
=
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'Bruxelles'
));
$this
->
assertInstanceOf
(
'Address'
,
$address
);
$this
->
assertInstanceOf
(
'MongoID'
,
$address
->
_id
);
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
$freshUser
=
User
::
find
(
$user
->
id
);
$freshUser
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$freshUser
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$freshUser
->
addresses
->
lists
(
'city'
));
$user
=
User
::
create
(
array
());
$address
=
$user
->
addresses
()
->
create
(
array
(
'_id'
=>
''
,
'city'
=>
'Bruxelles'
));
$this
->
assertInstanceOf
(
'MongoID'
,
$address
->
_id
);
}
public
function
testEmbedsManyCreateMany
()
{
$user
=
User
::
create
(
array
());
list
(
$bruxelles
,
$paris
)
=
$user
->
addresses
()
->
createMany
(
array
(
array
(
'city'
=>
'Bruxelles'
),
array
(
'city'
=>
'Paris'
)));
$this
->
assertInstanceOf
(
'Address'
,
$bruxelles
);
$this
->
assertEquals
(
'Bruxelles'
,
$bruxelles
->
city
);
$this
->
assertEquals
(
array
(
'Bruxelles'
,
'Paris'
),
$user
->
addresses
->
lists
(
'city'
));
$freshUser
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
array
(
'Bruxelles'
,
'Paris'
),
$freshUser
->
addresses
->
lists
(
'city'
));
}
}
public
function
testEmbedsManyDestroy
()
public
function
testEmbedsManyDestroy
()
...
...
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