Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Allow base collections in BelongsToMany::sync #16882

Merged
merged 2 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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