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
a8feeb78
Commit
a8feeb78
authored
Mar 02, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use push and pull for embedsMany
parent
82c23baa
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
16 deletions
+37
-16
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+10
-13
RelationsTest.php
tests/RelationsTest.php
+27
-3
No files found.
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
a8feeb78
...
@@ -179,14 +179,18 @@ class EmbedsMany extends Relation {
...
@@ -179,14 +179,18 @@ class EmbedsMany extends Relation {
$model
->
exists
=
true
;
$model
->
exists
=
true
;
// Push the document to the database.
$result
=
$this
->
parent
->
push
(
$this
->
localKey
,
$model
->
getAttributes
(),
true
);
// Get existing embedded documents.
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$documents
=
$this
->
getEmbedded
();
// Add the document to the parent model.
$documents
[]
=
$model
->
getAttributes
();
$documents
[]
=
$model
->
getAttributes
();
$this
->
setEmbedded
(
$documents
);
$this
->
setEmbedded
(
$documents
);
return
$
this
->
parent
->
save
()
?
$model
:
false
;
return
$
result
?
$model
:
false
;
}
}
/**
/**
...
@@ -205,22 +209,14 @@ class EmbedsMany extends Relation {
...
@@ -205,22 +209,14 @@ class EmbedsMany extends Relation {
$model
->
setUpdatedAt
(
$time
);
$model
->
setUpdatedAt
(
$time
);
}
}
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$key
=
$model
->
getKey
();
$key
=
$model
->
getKey
();
$primaryKey
=
$model
->
getKeyName
();
$primaryKey
=
$model
->
getKeyName
();
// Update timestamps.
// Get existing embedded documents.
if
(
$model
->
usesTimestamps
())
$documents
=
$this
->
getEmbedded
();
{
$time
=
$model
->
freshTimestamp
();
$model
->
setUpdatedAt
(
$time
);
}
// Replace the document
// Replace the document
in the parent model.
foreach
(
$documents
as
$i
=>
$document
)
foreach
(
$documents
as
$i
=>
$document
)
{
{
if
(
$document
[
$primaryKey
]
==
$key
)
if
(
$document
[
$primaryKey
]
==
$key
)
...
@@ -309,6 +305,7 @@ class EmbedsMany extends Relation {
...
@@ -309,6 +305,7 @@ class EmbedsMany extends Relation {
$primaryKey
=
$this
->
related
->
getKeyName
();
$primaryKey
=
$this
->
related
->
getKeyName
();
// Remove the document from the parent model.
foreach
(
$documents
as
$i
=>
$document
)
foreach
(
$documents
as
$i
=>
$document
)
{
{
if
(
in_array
(
$document
[
$primaryKey
],
$ids
))
if
(
in_array
(
$document
[
$primaryKey
],
$ids
))
...
@@ -369,7 +366,7 @@ class EmbedsMany extends Relation {
...
@@ -369,7 +366,7 @@ class EmbedsMany extends Relation {
{
{
$attributes
=
$this
->
parent
->
getAttributes
();
$attributes
=
$this
->
parent
->
getAttributes
();
$attributes
[
$this
->
localKey
]
=
$models
;
$attributes
[
$this
->
localKey
]
=
array_values
(
$models
)
;
// Set raw attributes to skip mutators.
// Set raw attributes to skip mutators.
$this
->
parent
->
setRawAttributes
(
$attributes
);
$this
->
parent
->
setRawAttributes
(
$attributes
);
...
...
tests/RelationsTest.php
View file @
a8feeb78
...
@@ -18,7 +18,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -18,7 +18,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Photo
::
truncate
();
Photo
::
truncate
();
}
}
public
function
testHasMany
()
/*
public function testHasMany()
{
{
$author = User::create(array('name' => 'George R. R. Martin'));
$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 Game of Thrones', 'author_id' => $author->_id));
...
@@ -281,7 +281,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -281,7 +281,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$photo = Photo::first();
$photo = Photo::first();
$this->assertEquals($photo->imageable->name, $user->name);
$this->assertEquals($photo->imageable->name, $user->name);
}
}
*/
public
function
testEmbedsManySave
()
public
function
testEmbedsManySave
()
{
{
...
@@ -312,6 +312,17 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -312,6 +312,17 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
created_at
);
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
created_at
);
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
updated_at
);
$this
->
assertInstanceOf
(
'DateTime'
,
$address
->
updated_at
);
$this
->
assertInstanceOf
(
'User'
,
$address
->
user
);
$this
->
assertInstanceOf
(
'User'
,
$address
->
user
);
$user
=
User
::
find
(
$user
->
_id
);
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Bruxelles'
)));
$this
->
assertEquals
(
array
(
'London'
,
'New York'
,
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
$address
=
$user
->
addresses
[
1
];
$address
->
city
=
"Manhattan"
;
$user
->
addresses
()
->
save
(
$address
);
$this
->
assertEquals
(
array
(
'London'
,
'Manhattan'
,
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
$freshUser
=
User
::
find
(
$user
->
_id
);
$this
->
assertEquals
(
array
(
'London'
,
'Manhattan'
,
'Bruxelles'
),
$freshUser
->
addresses
->
lists
(
'city'
));
}
}
public
function
testEmbedsManySaveMany
()
public
function
testEmbedsManySaveMany
()
...
@@ -326,7 +337,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -326,7 +337,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
public
function
testEmbedsManyCreate
()
public
function
testEmbedsManyCreate
()
{
{
$user
=
new
User
(
array
(
'name'
=>
'John Doe'
));
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'Bruxelles'
));
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'Bruxelles'
));
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
...
@@ -346,6 +357,19 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -346,6 +357,19 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$address
=
$user
->
addresses
->
first
();
$address
=
$user
->
addresses
->
first
();
$user
->
addresses
()
->
destroy
(
$address
);
$user
->
addresses
()
->
destroy
(
$address
);
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruxelles'
),
$user
->
addresses
->
lists
(
'city'
));
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'Paris'
));
$user
->
addresses
()
->
create
(
array
(
'city'
=>
'San Francisco'
));
$user
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
array
(
'Bruxelles'
,
'Paris'
,
'San Francisco'
),
$user
->
addresses
->
lists
(
'city'
));
$ids
=
$user
->
addresses
->
lists
(
'_id'
);
$user
->
addresses
()
->
destroy
(
$ids
);
$this
->
assertEquals
(
array
(),
$user
->
addresses
->
lists
(
'city'
));
$freshUser
=
User
::
find
(
$user
->
id
);
$this
->
assertEquals
(
array
(),
$freshUser
->
addresses
->
lists
(
'city'
));
}
}
public
function
testEmbedsManyAliases
()
public
function
testEmbedsManyAliases
()
...
...
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