Skip to content

Commit ec648d1

Browse files
committed
Merge branch 'master' of github.com:jenssegers/Laravel-MongoDB
2 parents 2904008 + e723e6c commit ec648d1

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ public function detach($ids = array(), $touch = true)
170170

171171
$query = $this->newParentQuery();
172172

173+
// Generate a new related query instance
174+
$related = $this->related->newInstance();
175+
173176
// If associated IDs were passed to the method we will only delete those
174177
// associations, otherwise all of the association ties will be broken.
175178
// We'll return the numbers of affected rows when we do the deletes.
@@ -189,6 +192,9 @@ public function detach($ids = array(), $touch = true)
189192
{
190193
$query->pull($this->otherKey, $id);
191194
}
195+
196+
// Remove the relation from the related model
197+
$related->pull($this->foreignKey, $this->parent->getKey());
192198

193199
return count($ids);
194200
}

tests/RelationsTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ public function testHasManyAndBelongsToAttachesExistingModels()
204204
);
205205

206206
$moreClients = array(
207-
Client::create(array('name' => 'Boloni Ltd.'))->_id,
208-
Client::create(array('name' => 'Meatballs Inc.'))->_id
207+
Client::create(array('name' => 'synced Boloni Ltd.'))->_id,
208+
Client::create(array('name' => 'synced Meatballs Inc.'))->_id
209209
);
210210

211211
// Sync multiple records
@@ -224,7 +224,10 @@ public function testHasManyAndBelongsToAttachesExistingModels()
224224

225225
$user = User::with('clients')->find($user->_id);
226226

227-
// Assert there are now 4 client objects in the relationship
228-
$this->assertCount(4, $user->clients);
227+
// Assert there are now still 2 client objects in the relationship
228+
$this->assertCount(2, $user->clients);
229+
// Assert that the new relationships name start with synced
230+
$this->assertStringStartsWith('synced', $user->clients[0]->name);
231+
$this->assertStringStartsWith('synced', $user->clients[1]->name);
229232
}
230233
}

0 commit comments

Comments
 (0)