diff --git a/.gitignore b/.gitignore index a4a4579c3..afe97b1e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store phpunit.phar /vendor +/.settings composer.phar composer.lock *.sublime-project diff --git a/README.md b/README.md index ce88b8144..88f7370fb 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ If you are using a different database driver as the default one, you will need t Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent ### Optional: Alias +------------------- You may also register an alias for the MongoDB model by adding the following to the alias array in `app/config/app.php`: @@ -367,4 +368,4 @@ By default, Laravel keeps a log in memory of all queries that have been run for DB::connection()->disableQueryLog(); -*From: http://laravel.com/docs/database#query-logging* +*From: http://laravel.com/docs/database#query-logging* \ No newline at end of file diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 58ba8e58f..4a1dd3a14 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -167,9 +167,16 @@ protected function createAttachRecords($ids, array $attributes) public function detach($ids = array(), $touch = true) { if ($ids instanceof Model) $ids = (array) $ids->getKey(); - + + // Generate a new parent query $query = $this->newParentQuery(); - + + // Generate a new related query instance + $related = $this->related->newInstance(); + + // Remove the relation from the related model + $related->pull($this->foreignKey, $this->parent->getKey()); + // 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. diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 6492ecf33..c6b16fbfa 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -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 @@ -209,7 +209,9 @@ 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); + $this->assertStringStartsWith('synced', $user->clients[0]->name); + $this->assertStringStartsWith('synced', $user->clients[1]->name); } }