Skip to content

Commit

Permalink
[5.3] Allow base collections in BelongsToMany::sync (#16882)
Browse files Browse the repository at this point in the history
* Allow base collections in BelongsToMany::sync

* Updated docs
  • Loading branch information
sebastiandedeyne authored and taylorotwell committed Dec 20, 2016
1 parent dd778cb commit 7baa075
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class BelongsToMany extends Relation
Expand Down Expand Up @@ -872,7 +873,7 @@ public function syncWithoutDetaching($ids)
/**
* Sync the intermediate tables with a list of IDs or collection of models.
*
* @param \Illuminate\Database\Eloquent\Collection|array $ids
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids
* @param bool $detaching
* @return array
*/
Expand All @@ -886,6 +887,10 @@ public function sync($ids, $detaching = true)
$ids = $ids->modelKeys();
}

if ($ids instanceof BaseCollection) {
$ids = $ids->toArray();
}

// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
Expand Down
17 changes: 16 additions & 1 deletion tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ public function testTouchIfTouching()
$relation->touchIfTouching();
}

public function testSyncMethodConvertsCollectionToArrayOfKeys()
public function testSyncMethodConvertsEloquentCollectionToArrayOfKeys()
{
$relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['attach', 'detach', 'touchIfTouching', 'formatRecordsList'])->setConstructorArgs($this->getRelationArguments())->getMock();
$query = m::mock('stdClass');
Expand All @@ -647,6 +647,21 @@ public function testSyncMethodConvertsCollectionToArrayOfKeys()
$relation->sync($collection);
}

public function testSyncMethodConvertsBaseCollectionToArrayOfKeys()
{
$relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['attach', 'detach', 'touchIfTouching', 'formatRecordsList'])->setConstructorArgs($this->getRelationArguments())->getMock();
$query = m::mock('stdClass');
$query->shouldReceive('from')->once()->with('user_role')->andReturn($query);
$query->shouldReceive('where')->once()->with('user_id', 1)->andReturn($query);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
$query->shouldReceive('pluck')->once()->with('role_id')->andReturn(new BaseCollection([1, 2, 3]));

$collection = new BaseCollection([1, 2, 3]);
$relation->expects($this->once())->method('formatRecordsList')->with([1, 2, 3])->will($this->returnValue([1 => [], 2 => [], 3 => []]));
$relation->sync($collection);
}

public function testWherePivotParamsUsedForNewQueries()
{
$relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['attach', 'detach', 'touchIfTouching', 'formatRecordsList'])->setConstructorArgs($this->getRelationArguments())->getMock();
Expand Down

0 comments on commit 7baa075

Please sign in to comment.