Skip to content

Commit c1af77a

Browse files
committed
Fix belongsToMany, fixes #147
1 parent 92b2299 commit c1af77a

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function dropColumn($columns)
238238
}
239239

240240
/**
241-
* Pass push to the query builder.
241+
* Append one or more values to an array.
242242
*
243243
* @return mixed
244244
*/
@@ -254,6 +254,18 @@ public function push()
254254
return parent::push();
255255
}
256256

257+
/**
258+
* Remove one or more values from an array.
259+
*
260+
* @return mixed
261+
*/
262+
public function pull()
263+
{
264+
$query = $this->setKeysForSaveQuery($this->newQuery());
265+
266+
return call_user_func_array(array($query, 'pull'), func_get_args());
267+
}
268+
257269
/**
258270
* Create a new Eloquent query builder for the model.
259271
*

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ public function addConstraints()
3636
{
3737
if (static::$constraints)
3838
{
39-
// Make sure that the primary key of the parent
40-
// is in the relationship array of keys
41-
$this->query->whereIn($this->foreignKey, array($this->parent->getKey()));
39+
$this->query->where($this->foreignKey, $this->parent->getKey());
4240
}
4341
}
4442

@@ -114,26 +112,25 @@ public function attach($id, array $attributes = array(), $touch = true)
114112
{
115113
if ($id instanceof Model) $id = $id->getKey();
116114

117-
// Generate a new parent query instance
118-
$parent = $this->newParentQuery();
119-
120-
// Generate a new related query instance
121-
$related = $this->related->newInstance();
122-
123-
// Set contraints on the related query
124-
$related = $related->where($this->related->getKeyName(), $id);
125-
126115
$records = $this->createAttachRecords((array) $id, $attributes);
127116

128117
// Get the ID's to attach to the two documents
129118
$otherIds = array_pluck($records, $this->otherKey);
130119
$foreignIds = array_pluck($records, $this->foreignKey);
131120

132121
// Attach to the parent model
133-
$parent->push($this->otherKey, $otherIds[0]);
122+
$this->parent->push($this->otherKey, $otherIds[0]);
123+
124+
// Generate a new related query instance
125+
$query = $this->getNewRelatedQuery();
126+
127+
// Set contraints on the related query
128+
$query->where($this->related->getKeyName(), $id);
134129

135130
// Attach to the related model
136-
$related->push($this->foreignKey, $foreignIds[0]);
131+
$query->push($this->foreignKey, $foreignIds[0]);
132+
133+
if ($touch) $this->touchIfTouching();
137134
}
138135

139136
/**
@@ -168,47 +165,32 @@ public function detach($ids = array(), $touch = true)
168165
{
169166
if ($ids instanceof Model) $ids = (array) $ids->getKey();
170167

171-
$query = $this->newParentQuery();
172-
173-
// Generate a new related query instance
174-
$related = $this->related->newInstance();
175-
176168
// If associated IDs were passed to the method we will only delete those
177169
// associations, otherwise all of the association ties will be broken.
178170
// We'll return the numbers of affected rows when we do the deletes.
179171
$ids = (array) $ids;
180172

181-
if (count($ids) > 0)
173+
// Pull each id from the parent.
174+
foreach ($ids as $id)
182175
{
183-
$query->whereIn($this->otherKey, $ids);
176+
$this->parent->pull($this->otherKey, $id);
184177
}
185178

186-
if ($touch) $this->touchIfTouching();
179+
// Get a new related query.
180+
$query = $this->getNewRelatedQuery();
187181

188-
// Once we have all of the conditions set on the statement, we are ready
189-
// to run the delete on the pivot table. Then, if the touch parameter
190-
// is true, we will go ahead and touch all related models to sync.
191-
foreach ($ids as $id)
182+
// Prepare the query to select all related objects.
183+
if (count($ids) > 0)
192184
{
193-
$query->pull($this->otherKey, $id);
185+
$query->whereIn($this->related->getKeyName(), $ids);
194186
}
195187

196-
// Remove the relation from the related model
197-
$related->pull($this->foreignKey, $this->parent->getKey());
198-
199-
return count($ids);
200-
}
188+
// Remove the relation to the parent.
189+
$query->pull($this->foreignKey, $this->parent->getKey());
201190

202-
/**
203-
* Create a new query builder for the parent
204-
*
205-
* @return Jenssegers\Mongodb\Builder
206-
*/
207-
protected function newParentQuery()
208-
{
209-
$query = $this->parent->newQuery();
191+
if ($touch) $this->touchIfTouching();
210192

211-
return $query->where($this->parent->getKeyName(), '=', $this->parent->getKey());
193+
return count($ids);
212194
}
213195

214196
/**
@@ -237,6 +219,16 @@ protected function buildDictionary(Collection $results)
237219
return $dictionary;
238220
}
239221

222+
/**
223+
* Get a new related query.
224+
*
225+
* @return \Illuminate\Database\Query\Builder
226+
*/
227+
public function getNewRelatedQuery()
228+
{
229+
return $this->related->newQuery();
230+
}
231+
240232
/**
241233
* Get the fully qualified foreign key for the relation.
242234
*

0 commit comments

Comments
 (0)