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
94314570
Commit
94314570
authored
Aug 10, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweak belongsToMany relation
parent
12abfb36
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
12 deletions
+63
-12
BelongsToMany.php
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
+60
-9
RelationsTest.php
tests/RelationsTest.php
+3
-3
No files found.
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
View file @
94314570
<?php
namespace
Jenssegers\Mongodb\Relations
;
<?php
namespace
Jenssegers\Mongodb\Relations
;
use
Jenssegers\Mongodb
\Model
;
use
Illuminate\Database\Eloquent
\Model
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Database\Eloquent\Relations\BelongsToMany
as
EloquentBelongsToMany
;
use
Illuminate\Database\Eloquent\Relations\BelongsToMany
as
EloquentBelongsToMany
;
...
@@ -40,6 +40,45 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -40,6 +40,45 @@ class BelongsToMany extends EloquentBelongsToMany {
}
}
}
}
/**
* Save a new model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public
function
save
(
Model
$model
,
array
$joining
=
array
(),
$touch
=
true
)
{
$model
->
save
(
array
(
'touch'
=>
false
));
$this
->
attach
(
$model
,
$joining
,
$touch
);
return
$model
;
}
/**
* Create a new instance of the related model.
*
* @param array $attributes
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public
function
create
(
array
$attributes
,
array
$joining
=
array
(),
$touch
=
true
)
{
$instance
=
$this
->
related
->
newInstance
(
$attributes
);
// Once we save the related model, we need to attach it to the base model via
// through intermediate table so we'll use the existing "attach" method to
// accomplish this which will insert the record and any more attributes.
$instance
->
save
(
array
(
'touch'
=>
false
));
$this
->
attach
(
$instance
,
$joining
,
$touch
);
return
$instance
;
}
/**
/**
* Sync the intermediate tables with a list of IDs or collection of models.
* Sync the intermediate tables with a list of IDs or collection of models.
*
*
...
@@ -102,7 +141,7 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -102,7 +141,7 @@ class BelongsToMany extends EloquentBelongsToMany {
*/
*/
public
function
updateExistingPivot
(
$id
,
array
$attributes
,
$touch
=
true
)
public
function
updateExistingPivot
(
$id
,
array
$attributes
,
$touch
=
true
)
{
{
//
TODO
//
Do nothing, we have no pivot table.
}
}
/**
/**
...
@@ -115,7 +154,10 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -115,7 +154,10 @@ class BelongsToMany extends EloquentBelongsToMany {
*/
*/
public
function
attach
(
$id
,
array
$attributes
=
array
(),
$touch
=
true
)
public
function
attach
(
$id
,
array
$attributes
=
array
(),
$touch
=
true
)
{
{
if
(
$id
instanceof
Model
)
$id
=
$id
->
getKey
();
if
(
$id
instanceof
Model
)
{
$model
=
$id
;
$id
=
$model
->
getKey
();
}
$records
=
$this
->
createAttachRecords
((
array
)
$id
,
$attributes
);
$records
=
$this
->
createAttachRecords
((
array
)
$id
,
$attributes
);
...
@@ -126,14 +168,23 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -126,14 +168,23 @@ class BelongsToMany extends EloquentBelongsToMany {
// Attach the new ids to the parent model.
// Attach the new ids to the parent model.
$this
->
parent
->
push
(
$this
->
otherKey
,
$otherIds
,
true
);
$this
->
parent
->
push
(
$this
->
otherKey
,
$otherIds
,
true
);
// Generate a new related query instance.
// If we have a model instance, we can psuh the ids to that model,
$query
=
$this
->
newRelatedQuery
();
// so that the internal attributes are updated as well. Otherwise,
// we will just perform a regular database query.
if
(
isset
(
$model
))
{
// Attach the new ids to the related model.
$model
->
push
(
$this
->
foreignKey
,
$foreignIds
,
true
);
}
else
{
$query
=
$this
->
newRelatedQuery
();
// Set contraints on the related query.
$query
->
where
(
$this
->
related
->
getKeyName
(),
$id
);
$query
->
where
(
$this
->
related
->
getKeyName
(),
$id
);
// Attach the new ids to the related model.
// Attach the new ids to the related model.
$query
->
push
(
$this
->
foreignKey
,
$foreignIds
,
true
);
$query
->
push
(
$this
->
foreignKey
,
$foreignIds
,
true
);
}
if
(
$touch
)
$this
->
touchIfTouching
();
if
(
$touch
)
$this
->
touchIfTouching
();
}
}
...
...
tests/RelationsTest.php
View file @
94314570
...
@@ -455,20 +455,20 @@ class RelationsTest extends TestCase {
...
@@ -455,20 +455,20 @@ class RelationsTest extends TestCase {
$user
->
save
();
$user
->
save
();
$this
->
assertEquals
(
1
,
$user
->
clients
()
->
count
());
$this
->
assertEquals
(
1
,
$user
->
clients
()
->
count
());
//$this->assertEquals(array($user->_id), $client->user_ids); TODO
$this
->
assertEquals
(
array
(
$user
->
_id
),
$client
->
user_ids
);
$this
->
assertEquals
(
array
(
$client
->
_id
),
$user
->
client_ids
);
$this
->
assertEquals
(
array
(
$client
->
_id
),
$user
->
client_ids
);
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$client
=
Client
::
where
(
'name'
,
'Admins'
)
->
first
();
$client
=
Client
::
where
(
'name'
,
'Admins'
)
->
first
();
$this
->
assertEquals
(
1
,
$user
->
clients
()
->
count
());
$this
->
assertEquals
(
1
,
$user
->
clients
()
->
count
());
//$this->assertEquals(array($user->_id), $client->user_ids); TODO
$this
->
assertEquals
(
array
(
$user
->
_id
),
$client
->
user_ids
);
$this
->
assertEquals
(
array
(
$client
->
_id
),
$user
->
client_ids
);
$this
->
assertEquals
(
array
(
$client
->
_id
),
$user
->
client_ids
);
$user
->
clients
()
->
save
(
$client
);
$user
->
clients
()
->
save
(
$client
);
$user
->
clients
()
->
save
(
$client
);
$user
->
clients
()
->
save
(
$client
);
$user
->
save
();
$user
->
save
();
$this
->
assertEquals
(
1
,
$user
->
clients
()
->
count
());
$this
->
assertEquals
(
1
,
$user
->
clients
()
->
count
());
//$this->assertEquals(array($user->_id), $client->user_ids); TODO
$this
->
assertEquals
(
array
(
$user
->
_id
),
$client
->
user_ids
);
$this
->
assertEquals
(
array
(
$client
->
_id
),
$user
->
client_ids
);
$this
->
assertEquals
(
array
(
$client
->
_id
),
$user
->
client_ids
);
}
}
...
...
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