Skip to content

Fixed sync bug #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Nov 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Jenssegers/Mongodb/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public function detach($ids = array(), $touch = true)

$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
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
Expand All @@ -189,6 +192,9 @@ public function detach($ids = array(), $touch = true)
{
$query->pull($this->otherKey, $id);
}

// Remove the relation from the related model
$related->pull($this->foreignKey, $this->parent->getKey());

return count($ids);
}
Expand Down
11 changes: 7 additions & 4 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ public function testHasManyAndBelongsToAttachesExistingModels()
);

$moreClients = array(
Client::create(array('name' => 'Boloni Ltd.'))->_id,
Client::create(array('name' => 'Meatballs Inc.'))->_id
Client::create(array('name' => 'synced Boloni Ltd.'))->_id,
Client::create(array('name' => 'synced Meatballs Inc.'))->_id
);

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

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

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