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
0caeeb9f
Commit
0caeeb9f
authored
Mar 08, 2014
by
Alexandre Butynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the associate method to EmbedsMany and refactoring
parent
a86d16a3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
22 deletions
+89
-22
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+68
-22
RelationsTest.php
tests/RelationsTest.php
+21
-0
No files found.
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
0caeeb9f
...
@@ -140,6 +140,8 @@ class EmbedsMany extends Relation {
...
@@ -140,6 +140,8 @@ class EmbedsMany extends Relation {
*/
*/
public
function
save
(
Model
$model
)
public
function
save
(
Model
$model
)
{
{
$this
->
updateTimestamps
(
$model
);
// Insert a new document.
// Insert a new document.
if
(
!
$model
->
exists
)
if
(
!
$model
->
exists
)
{
{
...
@@ -154,34 +156,40 @@ class EmbedsMany extends Relation {
...
@@ -154,34 +156,40 @@ class EmbedsMany extends Relation {
}
}
/**
/**
*
Perform a model insert operation
.
*
Attach a model instance to the parent model without persistence
.
*
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @return \Illuminate\Database\Eloquent\Model
*/
*/
p
rotected
function
performInsert
(
Model
$model
)
p
ublic
function
associate
(
Model
$model
)
{
{
//
Create a new key.
//
Insert the related model in the parent instance
if
(
!
$model
->
getAttribute
(
'_id'
)
)
if
(
!
$model
->
exists
)
{
{
$model
->
setAttribute
(
'_id'
,
new
MongoId
);
return
$this
->
associateNew
(
$model
);
}
}
// Update timestamps.
// Update the related model in the parent instance
$this
->
updateTimestamps
(
$model
);
else
{
// Push the document to the database.
return
$this
->
associateExisting
(
$model
);
$result
=
$this
->
query
->
push
(
$this
->
localKey
,
$model
->
getAttributes
(),
true
);
}
$documents
=
$this
->
getEmbeddedRecords
();
// Add the document to the parent model.
}
$documents
[]
=
$model
->
getAttributes
();
$this
->
setEmbeddedRecords
(
$documents
);
/**
* Perform a model insert operation.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
protected
function
performInsert
(
Model
$model
)
{
// Insert the related model in the parent instance
$this
->
associateNew
(
$model
);
//
Mark the model as existing
.
//
Push the document to the database
.
$
model
->
exists
=
true
;
$
result
=
$this
->
query
->
push
(
$this
->
localKey
,
$model
->
getAttributes
(),
true
)
;
return
$result
?
$model
:
false
;
return
$result
?
$model
:
false
;
}
}
...
@@ -194,8 +202,8 @@ class EmbedsMany extends Relation {
...
@@ -194,8 +202,8 @@ class EmbedsMany extends Relation {
*/
*/
protected
function
performUpdate
(
Model
$model
)
protected
function
performUpdate
(
Model
$model
)
{
{
// Update t
imestamps.
// Update t
he related model in the parent instance
$this
->
updateTimestamps
(
$model
);
$this
->
associateExisting
(
$model
);
// Get the correct foreign key value.
// Get the correct foreign key value.
$id
=
$this
->
getForeignKeyValue
(
$model
->
getKey
());
$id
=
$this
->
getForeignKeyValue
(
$model
->
getKey
());
...
@@ -204,6 +212,44 @@ class EmbedsMany extends Relation {
...
@@ -204,6 +212,44 @@ class EmbedsMany extends Relation {
$result
=
$this
->
query
->
where
(
$this
->
localKey
.
'.'
.
$model
->
getKeyName
(),
$id
)
$result
=
$this
->
query
->
where
(
$this
->
localKey
.
'.'
.
$model
->
getKeyName
(),
$id
)
->
update
(
array
(
$this
->
localKey
.
'.$'
=>
$model
->
getAttributes
()));
->
update
(
array
(
$this
->
localKey
.
'.$'
=>
$model
->
getAttributes
()));
return
$result
?
$model
:
false
;
}
/**
* Attach a new model without persistence
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
protected
function
associateNew
(
$model
)
{
// Create a new key.
if
(
!
$model
->
getAttribute
(
'_id'
))
{
$model
->
setAttribute
(
'_id'
,
new
MongoId
);
}
$documents
=
$this
->
getEmbeddedRecords
();
// Add the document to the parent model.
$documents
[]
=
$model
->
getAttributes
();
$this
->
setEmbeddedRecords
(
$documents
);
// Mark the model as existing.
$model
->
exists
=
true
;
return
$model
;
}
/**
* Update an existing model without persistence
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
protected
function
associateExisting
(
$model
)
{
// Get existing embedded documents.
// Get existing embedded documents.
$documents
=
$this
->
getEmbeddedRecords
();
$documents
=
$this
->
getEmbeddedRecords
();
...
@@ -212,18 +258,18 @@ class EmbedsMany extends Relation {
...
@@ -212,18 +258,18 @@ class EmbedsMany extends Relation {
$key
=
$model
->
getKey
();
$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
&
$document
)
{
{
if
(
$document
[
$primaryKey
]
==
$key
)
if
(
$document
[
$primaryKey
]
==
$key
)
{
{
$document
s
[
$i
]
=
$model
->
getAttributes
();
$document
=
$model
->
getAttributes
();
break
;
break
;
}
}
}
}
$this
->
setEmbeddedRecords
(
$documents
);
$this
->
setEmbeddedRecords
(
$documents
);
return
$
result
?
$model
:
false
;
return
$
model
;
}
}
/**
/**
...
...
tests/RelationsTest.php
View file @
0caeeb9f
...
@@ -329,6 +329,27 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -329,6 +329,27 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
array
(
'London'
,
'Manhattan'
,
'Bruxelles'
),
$freshUser
->
addresses
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'London'
,
'Manhattan'
,
'Bruxelles'
),
$freshUser
->
addresses
->
lists
(
'city'
));
}
}
public
function
testEmbedsManyAssociate
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$address
=
new
Address
(
array
(
'city'
=>
'London'
));
$address
=
$user
->
addresses
()
->
associate
(
$address
);
$this
->
assertNotNull
(
$user
->
_addresses
);
$this
->
assertEquals
(
array
(
'London'
),
$user
->
addresses
->
lists
(
'city'
));
$this
->
assertNotNull
(
$address
->
_id
);
$freshUser
=
User
::
find
(
$user
->
_id
);
$this
->
assertEquals
(
array
(),
$freshUser
->
addresses
->
lists
(
'city'
));
$address
->
city
=
'Londinium'
;
$user
->
addresses
()
->
associate
(
$address
);
$this
->
assertEquals
(
array
(
'Londinium'
),
$user
->
addresses
->
lists
(
'city'
));
$freshUser
=
User
::
find
(
$user
->
_id
);
$this
->
assertEquals
(
array
(),
$freshUser
->
addresses
->
lists
(
'city'
));
}
public
function
testEmbedsManySaveMany
()
public
function
testEmbedsManySaveMany
()
{
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
...
...
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