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
48ac180d
Commit
48ac180d
authored
Mar 02, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweaked embedsMany with more push/pull
parent
a8feeb78
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
19 deletions
+41
-19
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+1
-1
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+28
-9
RelationsTest.php
tests/RelationsTest.php
+12
-9
No files found.
src/Jenssegers/Mongodb/Query/Builder.php
View file @
48ac180d
...
@@ -640,7 +640,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -640,7 +640,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
* @param mixed $id
* @param mixed $id
* @return mixed
* @return mixed
*/
*/
p
rotected
function
convertKey
(
$id
)
p
ublic
function
convertKey
(
$id
)
{
{
if
(
is_string
(
$id
)
&&
strlen
(
$id
)
===
24
&&
ctype_xdigit
(
$id
))
if
(
is_string
(
$id
)
&&
strlen
(
$id
)
===
24
&&
ctype_xdigit
(
$id
))
{
{
...
...
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
48ac180d
...
@@ -55,6 +55,10 @@ class EmbedsMany extends Relation {
...
@@ -55,6 +55,10 @@ class EmbedsMany extends Relation {
*/
*/
public
function
addConstraints
()
public
function
addConstraints
()
{
{
if
(
static
::
$constraints
)
{
$this
->
query
->
where
(
$this
->
parent
->
getKeyName
(),
'='
,
$this
->
parent
->
getKey
());
}
}
}
/**
/**
...
@@ -180,7 +184,7 @@ class EmbedsMany extends Relation {
...
@@ -180,7 +184,7 @@ class EmbedsMany extends Relation {
$model
->
exists
=
true
;
$model
->
exists
=
true
;
// Push the document to the database.
// Push the document to the database.
$result
=
$this
->
parent
->
push
(
$this
->
localKey
,
$model
->
getAttributes
(),
true
);
$result
=
$this
->
query
->
push
(
$this
->
localKey
,
$model
->
getAttributes
(),
true
);
// Get existing embedded documents.
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$documents
=
$this
->
getEmbedded
();
...
@@ -209,13 +213,20 @@ class EmbedsMany extends Relation {
...
@@ -209,13 +213,20 @@ class EmbedsMany extends Relation {
$model
->
setUpdatedAt
(
$time
);
$model
->
setUpdatedAt
(
$time
);
}
}
$key
=
$model
->
getKey
();
// Convert the id to MongoId if necessary.
$id
=
$this
->
query
->
getQuery
()
->
convertKey
(
$model
->
getKey
());
$primaryKey
=
$model
->
getKeyName
();
// Update document in database.
$result
=
$this
->
query
->
where
(
$this
->
localKey
.
'.'
.
$model
->
getKeyName
(),
$id
)
->
update
(
array
(
$this
->
localKey
.
'.$'
=>
$model
->
getAttributes
()));
// Get existing embedded documents.
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$documents
=
$this
->
getEmbedded
();
$primaryKey
=
$this
->
related
->
getKeyName
();
$key
=
$model
->
getKey
();
// Replace the document in the parent model.
// Replace the document in the parent model.
foreach
(
$documents
as
$i
=>
$document
)
foreach
(
$documents
as
$i
=>
$document
)
{
{
...
@@ -228,7 +239,7 @@ class EmbedsMany extends Relation {
...
@@ -228,7 +239,7 @@ class EmbedsMany extends Relation {
$this
->
setEmbedded
(
$documents
);
$this
->
setEmbedded
(
$documents
);
return
$
this
->
parent
->
save
()
?
$model
:
false
;
return
$
result
?
$model
:
false
;
}
}
/**
/**
...
@@ -300,25 +311,33 @@ class EmbedsMany extends Relation {
...
@@ -300,25 +311,33 @@ class EmbedsMany extends Relation {
// We'll return the numbers of affected rows when we do the deletes.
// We'll return the numbers of affected rows when we do the deletes.
$ids
=
(
array
)
$ids
;
$ids
=
(
array
)
$ids
;
$primaryKey
=
$this
->
related
->
getKeyName
();
// Pull the documents from the database.
foreach
(
$ids
as
$id
)
{
// Convert the id to MongoId if necessary.
$id
=
$this
->
query
->
getQuery
()
->
convertKey
(
$id
);
$this
->
query
->
pull
(
$this
->
localKey
,
array
(
$primaryKey
=>
$id
));
$count
++
;
}
// Get existing embedded documents.
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$documents
=
$this
->
getEmbedded
();
$primaryKey
=
$this
->
related
->
getKeyName
();
// Remove the document from the parent model.
// 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
))
{
{
unset
(
$documents
[
$i
]);
unset
(
$documents
[
$i
]);
$count
++
;
}
}
}
}
$this
->
setEmbedded
(
$documents
);
$this
->
setEmbedded
(
$documents
);
$this
->
parent
->
save
();
return
$count
;
return
$count
;
}
}
...
...
tests/RelationsTest.php
View file @
48ac180d
...
@@ -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
()
{
{
...
@@ -296,18 +296,21 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -296,18 +296,21 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$address
=
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
)));
$address
=
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
)));
$
freshU
ser
=
User
::
find
(
$user
->
_id
);
$
u
ser
=
User
::
find
(
$user
->
_id
);
$this
->
assertEquals
(
array
(
'London'
,
'Paris'
),
$
freshU
ser
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'London'
,
'Paris'
),
$
u
ser
->
addresses
->
lists
(
'city'
));
$address
->
city
=
'New York'
;
$address
->
city
=
'New York'
;
$
freshU
ser
->
addresses
()
->
save
(
$address
);
$
u
ser
->
addresses
()
->
save
(
$address
);
$this
->
assertEquals
(
2
,
count
(
$freshUser
->
addresses
));
$this
->
assertEquals
(
2
,
count
(
$user
->
addresses
));
$this
->
assertEquals
(
2
,
count
(
$freshUser
->
addresses
()
->
get
()));
$this
->
assertEquals
(
2
,
count
(
$user
->
addresses
()
->
get
()));
$this
->
assertEquals
(
2
,
$freshUser
->
addresses
->
count
());
$this
->
assertEquals
(
2
,
$user
->
addresses
->
count
());
$this
->
assertEquals
(
array
(
'London'
,
'New York'
),
$user
->
addresses
->
lists
(
'city'
));
$freshUser
=
User
::
find
(
$user
->
_id
);
$this
->
assertEquals
(
array
(
'London'
,
'New York'
),
$freshUser
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'London'
,
'New York'
),
$freshUser
->
addresses
->
lists
(
'city'
));
$address
=
$
freshU
ser
->
addresses
->
first
();
$address
=
$
u
ser
->
addresses
->
first
();
$this
->
assertEquals
(
'London'
,
$address
->
city
);
$this
->
assertEquals
(
'London'
,
$address
->
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
);
...
...
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