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
c1af77aa
Commit
c1af77aa
authored
Mar 04, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix belongsToMany, fixes #147
parent
92b2299c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
42 deletions
+46
-42
Model.php
src/Jenssegers/Mongodb/Model.php
+13
-1
BelongsToMany.php
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
+33
-41
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
c1af77aa
...
@@ -238,7 +238,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -238,7 +238,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
}
}
/**
/**
*
Pass push to the query builder
.
*
Append one or more values to an array
.
*
*
* @return mixed
* @return mixed
*/
*/
...
@@ -254,6 +254,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -254,6 +254,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return
parent
::
push
();
return
parent
::
push
();
}
}
/**
* Remove one or more values from an array.
*
* @return mixed
*/
public
function
pull
()
{
$query
=
$this
->
setKeysForSaveQuery
(
$this
->
newQuery
());
return
call_user_func_array
(
array
(
$query
,
'pull'
),
func_get_args
());
}
/**
/**
* Create a new Eloquent query builder for the model.
* Create a new Eloquent query builder for the model.
*
*
...
...
src/Jenssegers/Mongodb/Relations/BelongsToMany.php
View file @
c1af77aa
...
@@ -36,9 +36,7 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -36,9 +36,7 @@ class BelongsToMany extends EloquentBelongsToMany {
{
{
if
(
static
::
$constraints
)
if
(
static
::
$constraints
)
{
{
// Make sure that the primary key of the parent
$this
->
query
->
where
(
$this
->
foreignKey
,
$this
->
parent
->
getKey
());
// is in the relationship array of keys
$this
->
query
->
whereIn
(
$this
->
foreignKey
,
array
(
$this
->
parent
->
getKey
()));
}
}
}
}
...
@@ -114,15 +112,6 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -114,15 +112,6 @@ class BelongsToMany extends EloquentBelongsToMany {
{
{
if
(
$id
instanceof
Model
)
$id
=
$id
->
getKey
();
if
(
$id
instanceof
Model
)
$id
=
$id
->
getKey
();
// Generate a new parent query instance
$parent
=
$this
->
newParentQuery
();
// Generate a new related query instance
$related
=
$this
->
related
->
newInstance
();
// Set contraints on the related query
$related
=
$related
->
where
(
$this
->
related
->
getKeyName
(),
$id
);
$records
=
$this
->
createAttachRecords
((
array
)
$id
,
$attributes
);
$records
=
$this
->
createAttachRecords
((
array
)
$id
,
$attributes
);
// Get the ID's to attach to the two documents
// Get the ID's to attach to the two documents
...
@@ -130,10 +119,18 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -130,10 +119,18 @@ class BelongsToMany extends EloquentBelongsToMany {
$foreignIds
=
array_pluck
(
$records
,
$this
->
foreignKey
);
$foreignIds
=
array_pluck
(
$records
,
$this
->
foreignKey
);
// Attach to the parent model
// Attach to the parent model
$parent
->
push
(
$this
->
otherKey
,
$otherIds
[
0
]);
$this
->
parent
->
push
(
$this
->
otherKey
,
$otherIds
[
0
]);
// Generate a new related query instance
$query
=
$this
->
getNewRelatedQuery
();
// Set contraints on the related query
$query
->
where
(
$this
->
related
->
getKeyName
(),
$id
);
// Attach to the related model
// Attach to the related model
$related
->
push
(
$this
->
foreignKey
,
$foreignIds
[
0
]);
$query
->
push
(
$this
->
foreignKey
,
$foreignIds
[
0
]);
if
(
$touch
)
$this
->
touchIfTouching
();
}
}
/**
/**
...
@@ -168,47 +165,32 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -168,47 +165,32 @@ class BelongsToMany extends EloquentBelongsToMany {
{
{
if
(
$ids
instanceof
Model
)
$ids
=
(
array
)
$ids
->
getKey
();
if
(
$ids
instanceof
Model
)
$ids
=
(
array
)
$ids
->
getKey
();
$query
=
$this
->
newParentQuery
();
// Generate a new related query instance
$related
=
$this
->
related
->
newInstance
();
// If associated IDs were passed to the method we will only delete those
// If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken.
// associations, otherwise all of the association ties will be broken.
// 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
;
if
(
count
(
$ids
)
>
0
)
// Pull each id from the parent.
foreach
(
$ids
as
$id
)
{
{
$
query
->
whereIn
(
$this
->
otherKey
,
$ids
);
$
this
->
parent
->
pull
(
$this
->
otherKey
,
$id
);
}
}
if
(
$touch
)
$this
->
touchIfTouching
();
// Get a new related query.
$query
=
$this
->
getNewRelatedQuery
();
// Once we have all of the conditions set on the statement, we are ready
// Prepare the query to select all related objects.
// to run the delete on the pivot table. Then, if the touch parameter
if
(
count
(
$ids
)
>
0
)
// is true, we will go ahead and touch all related models to sync.
foreach
(
$ids
as
$id
)
{
{
$query
->
pull
(
$this
->
otherKey
,
$id
);
$query
->
whereIn
(
$this
->
related
->
getKeyName
(),
$ids
);
}
}
// Remove the relation from the related model
// Remove the relation to the parent.
$related
->
pull
(
$this
->
foreignKey
,
$this
->
parent
->
getKey
());
$query
->
pull
(
$this
->
foreignKey
,
$this
->
parent
->
getKey
());
return
count
(
$ids
);
}
/**
if
(
$touch
)
$this
->
touchIfTouching
();
* Create a new query builder for the parent
*
* @return Jenssegers\Mongodb\Builder
*/
protected
function
newParentQuery
()
{
$query
=
$this
->
parent
->
newQuery
();
return
$query
->
where
(
$this
->
parent
->
getKeyName
(),
'='
,
$this
->
parent
->
getKey
()
);
return
count
(
$ids
);
}
}
/**
/**
...
@@ -237,6 +219,16 @@ class BelongsToMany extends EloquentBelongsToMany {
...
@@ -237,6 +219,16 @@ class BelongsToMany extends EloquentBelongsToMany {
return
$dictionary
;
return
$dictionary
;
}
}
/**
* Get a new related query.
*
* @return \Illuminate\Database\Query\Builder
*/
public
function
getNewRelatedQuery
()
{
return
$this
->
related
->
newQuery
();
}
/**
/**
* Get the fully qualified foreign key for the relation.
* Get the fully qualified foreign key for the relation.
*
*
...
...
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