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
19e9e9f5
Commit
19e9e9f5
authored
May 27, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding associate for embedsOne, fixes #215
parent
96f07a9f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
2 deletions
+36
-2
EmbedsOne.php
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
+21
-2
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+15
-0
No files found.
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
View file @
19e9e9f5
...
@@ -35,6 +35,25 @@ class EmbedsOne extends EmbedsOneOrMany {
...
@@ -35,6 +35,25 @@ class EmbedsOne extends EmbedsOneOrMany {
return
(
$embedded
and
$embedded
[
$primaryKey
]
==
$key
);
return
(
$embedded
and
$embedded
[
$primaryKey
]
==
$key
);
}
}
/**
* Associate the model instance to the given parent, without saving it to the database.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
public
function
associate
(
Model
$model
)
{
// Create a new key if needed.
if
(
!
$model
->
getAttribute
(
'_id'
))
{
$model
->
setAttribute
(
'_id'
,
new
MongoId
);
}
$this
->
setEmbedded
(
$model
->
getAttributes
());
return
$model
;
}
/**
/**
* Save a new model and attach it to the parent model.
* Save a new model and attach it to the parent model.
*
*
...
@@ -51,7 +70,7 @@ class EmbedsOne extends EmbedsOneOrMany {
...
@@ -51,7 +70,7 @@ class EmbedsOne extends EmbedsOneOrMany {
$result
=
$this
->
query
->
update
(
array
(
$this
->
localKey
=>
$model
->
getAttributes
()));
$result
=
$this
->
query
->
update
(
array
(
$this
->
localKey
=>
$model
->
getAttributes
()));
if
(
$result
)
$this
->
setEmbedded
(
$model
->
getAttributes
()
);
if
(
$result
)
$this
->
associate
(
$model
);
return
$result
?
$model
:
false
;
return
$result
?
$model
:
false
;
}
}
...
@@ -66,7 +85,7 @@ class EmbedsOne extends EmbedsOneOrMany {
...
@@ -66,7 +85,7 @@ class EmbedsOne extends EmbedsOneOrMany {
{
{
$result
=
$this
->
query
->
update
(
array
(
$this
->
localKey
=>
$model
->
getAttributes
()));
$result
=
$this
->
query
->
update
(
array
(
$this
->
localKey
=>
$model
->
getAttributes
()));
if
(
$result
)
$this
->
setEmbedded
(
$model
->
getAttributes
()
);
if
(
$result
)
$this
->
associate
(
$model
);
return
$result
?
$model
:
false
;
return
$result
?
$model
:
false
;
}
}
...
...
tests/EmbeddedRelationsTest.php
View file @
19e9e9f5
...
@@ -414,6 +414,21 @@ class EmbeddedRelationsTest extends TestCase {
...
@@ -414,6 +414,21 @@ class EmbeddedRelationsTest extends TestCase {
$this
->
assertEquals
(
'Jim Doe'
,
$user
->
father
->
name
);
$this
->
assertEquals
(
'Jim Doe'
,
$user
->
father
->
name
);
}
}
public
function
testEmbedsOneAssociate
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$father
=
new
User
(
array
(
'name'
=>
'Mark Doe'
));
$father
->
setEventDispatcher
(
$events
=
Mockery
::
mock
(
'Illuminate\Events\Dispatcher'
));
$events
->
shouldReceive
(
'until'
)
->
times
(
0
)
->
with
(
'eloquent.saving: '
.
get_class
(
$father
),
$father
);
$father
=
$user
->
father
()
->
associate
(
$father
);
$father
->
unsetEventDispatcher
();
$this
->
assertNotNull
(
$user
->
_father
);
$this
->
assertEquals
(
'Mark Doe'
,
$user
->
father
->
name
);
}
public
function
testEmbedsOneDelete
()
public
function
testEmbedsOneDelete
()
{
{
$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