Skip to content

Commit 84d8fd5

Browse files
committed
Fixes #439
1 parent a4712ac commit 84d8fd5

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function sync($ids, $detaching = true)
104104

105105
$records = $this->formatSyncList($ids);
106106

107-
$detach = array_diff($current, array_keys($records));
107+
$detach = array_values(array_diff($current, array_keys($records)));
108108

109109
// Next, we will take the differences of the currents and given IDs and detach
110110
// all of the entities that exist in the "current" array but are not in the
@@ -159,31 +159,28 @@ public function attach($id, array $attributes = array(), $touch = true)
159159
$model = $id; $id = $model->getKey();
160160
}
161161

162-
$records = $this->createAttachRecords((array) $id, $attributes);
163-
164-
// Get the ids to attach to the parent and related model.
165-
$otherIds = array_pluck($records, $this->otherKey);
166-
$foreignIds = array_pluck($records, $this->foreignKey);
162+
$ids = (array) $id;
167163

168164
// Attach the new ids to the parent model.
169-
$this->parent->push($this->otherKey, $otherIds, true);
165+
$this->parent->push($this->otherKey, $ids, true);
170166

171-
// If we have a model instance, we can psuh the ids to that model,
167+
// If we have a model instance, we can push the ids to that model,
172168
// so that the internal attributes are updated as well. Otherwise,
173169
// we will just perform a regular database query.
174170
if (isset($model))
175171
{
176172
// Attach the new ids to the related model.
177-
$model->push($this->foreignKey, $foreignIds, true);
173+
$model->push($this->foreignKey, $this->parent->getKey(), true);
178174
}
179175
else
180176
{
181177
$query = $this->newRelatedQuery();
182178

183-
$query->where($this->related->getKeyName(), $id);
179+
// Select related models.
180+
$query->whereIn($this->related->getKeyName(), $ids);
184181

185-
// Attach the new ids to the related model.
186-
$query->push($this->foreignKey, $foreignIds, true);
182+
// Attach the new parent id to the related model.
183+
$query->push($this->foreignKey, $this->parent->getKey(), true);
187184
}
188185

189186
if ($touch) $this->touchIfTouching();

tests/RelationsTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,34 @@ public function testBelongsToManySync()
277277
$this->assertCount(1, $user->clients);
278278
}
279279

280+
public function testBelongsToManyAttachArray()
281+
{
282+
$user = User::create(array('name' => 'John Doe'));
283+
$client1 = Client::create(array('name' => 'Test 1'))->_id;
284+
$client2 = Client::create(array('name' => 'Test 2'))->_id;
285+
286+
$user = User::where('name', '=', 'John Doe')->first();
287+
$user->clients()->attach([$client1, $client2]);
288+
$this->assertCount(2, $user->clients);
289+
}
290+
291+
public function testBelongsToManySyncAlreadyPresent()
292+
{
293+
$user = User::create(array('name' => 'John Doe'));
294+
$client1 = Client::create(array('name' => 'Test 1'))->_id;
295+
$client2 = Client::create(array('name' => 'Test 2'))->_id;
296+
297+
$user->clients()->sync([$client1, $client2]);
298+
$this->assertCount(2, $user->clients);
299+
300+
$user = User::where('name', '=', 'John Doe')->first();
301+
$user->clients()->sync([$client1]);
302+
$this->assertCount(1, $user->clients);
303+
304+
$user = User::where('name', '=', 'John Doe')->first()->toArray();
305+
$this->assertCount(1, $user['client_ids']);
306+
}
307+
280308
public function testBelongsToManyCustom()
281309
{
282310
$user = User::create(array('name' => 'John Doe'));

0 commit comments

Comments
 (0)