Skip to content

Commit 57c5bed

Browse files
committed
Merge pull request #76 from DyeH/master
Fixed sync bug
2 parents d709d4a + 545dde7 commit 57c5bed

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

+6
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

+7-4
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ public function testHasManyAndBelongsToAttachesExistingModels()
190190
);
191191

192192
$moreClients = array(
193-
Client::create(array('name' => 'Boloni Ltd.'))->_id,
194-
Client::create(array('name' => 'Meatballs Inc.'))->_id
193+
Client::create(array('name' => 'synced Boloni Ltd.'))->_id,
194+
Client::create(array('name' => 'synced Meatballs Inc.'))->_id
195195
);
196196

197197
// Sync multiple records
@@ -209,7 +209,10 @@ public function testHasManyAndBelongsToAttachesExistingModels()
209209

210210
$user = User::with('clients')->find($user->_id);
211211

212-
// Assert there are now 4 client objects in the relationship
213-
$this->assertCount(4, $user->clients);
212+
// Assert there are now still 2 client objects in the relationship
213+
$this->assertCount(2, $user->clients);
214+
// Assert that the new relationships name start with synced
215+
$this->assertStringStartsWith('synced', $user->clients[0]->name);
216+
$this->assertStringStartsWith('synced', $user->clients[1]->name);
214217
}
215218
}

0 commit comments

Comments
 (0)