Skip to content

Commit 2a15978

Browse files
committed
Fixed sync bug
1 parent d709d4a commit 2a15978

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)