Commit 84d8fd5e authored by Jens Segers's avatar Jens Segers

Fixes #439

parent a4712ac1
...@@ -104,7 +104,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -104,7 +104,7 @@ class BelongsToMany extends EloquentBelongsToMany {
$records = $this->formatSyncList($ids); $records = $this->formatSyncList($ids);
$detach = array_diff($current, array_keys($records)); $detach = array_values(array_diff($current, array_keys($records)));
// Next, we will take the differences of the currents and given IDs and detach // Next, we will take the differences of the currents and given IDs and detach
// all of the entities that exist in the "current" array but are not in the // all of the entities that exist in the "current" array but are not in the
...@@ -159,31 +159,28 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -159,31 +159,28 @@ class BelongsToMany extends EloquentBelongsToMany {
$model = $id; $id = $model->getKey(); $model = $id; $id = $model->getKey();
} }
$records = $this->createAttachRecords((array) $id, $attributes); $ids = (array) $id;
// Get the ids to attach to the parent and related model.
$otherIds = array_pluck($records, $this->otherKey);
$foreignIds = array_pluck($records, $this->foreignKey);
// Attach the new ids to the parent model. // Attach the new ids to the parent model.
$this->parent->push($this->otherKey, $otherIds, true); $this->parent->push($this->otherKey, $ids, true);
// If we have a model instance, we can psuh the ids to that model, // If we have a model instance, we can push the ids to that model,
// so that the internal attributes are updated as well. Otherwise, // so that the internal attributes are updated as well. Otherwise,
// we will just perform a regular database query. // we will just perform a regular database query.
if (isset($model)) if (isset($model))
{ {
// Attach the new ids to the related model. // Attach the new ids to the related model.
$model->push($this->foreignKey, $foreignIds, true); $model->push($this->foreignKey, $this->parent->getKey(), true);
} }
else else
{ {
$query = $this->newRelatedQuery(); $query = $this->newRelatedQuery();
$query->where($this->related->getKeyName(), $id); // Select related models.
$query->whereIn($this->related->getKeyName(), $ids);
// Attach the new ids to the related model. // Attach the new parent id to the related model.
$query->push($this->foreignKey, $foreignIds, true); $query->push($this->foreignKey, $this->parent->getKey(), true);
} }
if ($touch) $this->touchIfTouching(); if ($touch) $this->touchIfTouching();
......
...@@ -277,6 +277,34 @@ class RelationsTest extends TestCase { ...@@ -277,6 +277,34 @@ class RelationsTest extends TestCase {
$this->assertCount(1, $user->clients); $this->assertCount(1, $user->clients);
} }
public function testBelongsToManyAttachArray()
{
$user = User::create(array('name' => 'John Doe'));
$client1 = Client::create(array('name' => 'Test 1'))->_id;
$client2 = Client::create(array('name' => 'Test 2'))->_id;
$user = User::where('name', '=', 'John Doe')->first();
$user->clients()->attach([$client1, $client2]);
$this->assertCount(2, $user->clients);
}
public function testBelongsToManySyncAlreadyPresent()
{
$user = User::create(array('name' => 'John Doe'));
$client1 = Client::create(array('name' => 'Test 1'))->_id;
$client2 = Client::create(array('name' => 'Test 2'))->_id;
$user->clients()->sync([$client1, $client2]);
$this->assertCount(2, $user->clients);
$user = User::where('name', '=', 'John Doe')->first();
$user->clients()->sync([$client1]);
$this->assertCount(1, $user->clients);
$user = User::where('name', '=', 'John Doe')->first()->toArray();
$this->assertCount(1, $user['client_ids']);
}
public function testBelongsToManyCustom() public function testBelongsToManyCustom()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment