Skip to content

Commit db8fa9d

Browse files
committed
Cleaning Up Updates
1 parent fad2ef5 commit db8fa9d

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,32 @@ public function sync($ids, $detaching = true)
198198
*/
199199
public function updateExistingPivot($id, array $attributes, $touch = true)
200200
{
201-
// Do nothing, we have no pivot table.
201+
if ($id instanceof Model) {
202+
$model = $id;
203+
$id = $model->getKey();
204+
} else {
205+
if ($id instanceof Collection) {
206+
$id = $id->modelKeys();
207+
}
208+
209+
$related = $this->newRelatedQuery()->whereIn($this->related->getKeyName(), (array) $id);
210+
$filter = [$this->parentKey => $this->parent->getKey()];
211+
$pivot_x = [array_merge($attributes, $filter)];
212+
213+
//TODO: Put this in a transaction
214+
$related->pull($this->getForeignKey(), $this->parent->getKey());
215+
$related->pull($this->getForeignKey(), $filter);
216+
$related->push($this->getForeignKey(),$pivot_x,true);
217+
218+
}
219+
$filter = [$this->parentKey => $id];
220+
$pivot_x = [array_merge($attributes, $filter)];
221+
222+
//TODO: Put this in a transaction
223+
$this->parent->pull($this->getRelatedKey(), $id);
224+
$this->parent->pull($this->getRelatedKey(), $filter);
225+
$this->parent->push($this->getRelatedKey(), $pivot_x, true);
226+
202227
}
203228

204229
/**
@@ -265,14 +290,12 @@ public function detach($ids = [], $touch = true)
265290

266291
// Prepare the query to select all related objects.
267292
if (count($ids) > 0) {
268-
$query
269-
->whereIn($this->related->getKeyName(), $ids)
270-
->orWhereIn($this->related->getKeyName().'._id', $ids);
293+
$query->whereIn($this->related->getKeyName(), $ids);
271294
}
272295

273296
// Remove the relation to the parent.
274297
$query->pull($this->foreignPivotKey, $this->parent->getKey());
275-
$query->pull($this->foreignPivotKey, ['_id'=>$this->parent->getKey()]);
298+
$query->pull($this->foreignPivotKey, [$this->parentKey=>$this->parent->getKey()]);
276299

277300
if ($touch) {
278301
$this->touchIfTouching();

tests/RelationsTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,7 @@ public function testBelongsToManyAttachesExistingModels()
263263
$user->clients()->sync($moreClients);
264264

265265
// Refetch
266-
// $user = User::with('clients')->find($user->_id);
267-
/** @var User $user */
268-
$user = User::find($user->_id);
269-
$user->load('clients');
266+
$user = User::with('clients')->find($user->_id);
270267

271268
// Assert there are now still 2 client objects in the relationship
272269
$this->assertCount(2, $user->clients()->get());
@@ -304,7 +301,10 @@ public function testBelongsToManySyncAttrs()
304301
$client2 = Client::create(['name' => 'Buffet Bar Inc.'])->_id;
305302

306303
// Sync multiple
307-
$user->clients()->sync([$client1 => ['fresh_client' => true], $client2 => ['fresh_client' => false]]);
304+
$user->clients()->sync([
305+
$client1 => ['fresh_client' => true],
306+
$client2 => ['fresh_client' => false]
307+
]);
308308

309309
//Check Sync Success
310310
$this->assertEquals($user->client_ids[0]['_id'], $client1);
@@ -317,11 +317,26 @@ public function testBelongsToManySyncAttrs()
317317
$clients = $user->clients;
318318
$this->assertCount(2, $clients);
319319

320-
foreach ($user->clients()->withPivot('fresh_client')->get() as $item) {
320+
foreach ($user->clients()->get() as $item) {
321321
$this->assertIsArray($item->pivot->toArray());
322322
$this->assertEquals($item->_id == $client1, $item->pivot->fresh_client);
323323
}
324-
$this->assertTrue(true);
324+
}
325+
326+
public function testBelongsToManyUpdatePivot()
327+
{
328+
/** @var User $user */
329+
$user = User::create(['name' => 'John Doe']);
330+
$client1 = Client::create(['name' => 'Pork Pies Ltd.'])->_id;
331+
$client2 = Client::create(['name' => 'Buffet Bar Inc.'])->_id;
332+
333+
// Sync multiple
334+
$user->clients()->sync([
335+
$client1 => ['fresh_client' => true],
336+
$client2 => ['fresh_client' => false]
337+
]);
338+
$user->clients()->updateExistingPivot($client1,['fresh_client'=>false]);
339+
$this->assertFalse($user->clients()->where('_id',$client1)->first()->pivot->fresh_client);
325340
}
326341

327342
public function testBelongsToManyAttachArray()

0 commit comments

Comments
 (0)