Skip to content

Commit

Permalink
Merge pull request #28432 from mnabialek/findmany-avoid-query-when-em…
Browse files Browse the repository at this point in the history
…pty-ids

[5.8] Don't execute query when ids are empty Arrayable
  • Loading branch information
taylorotwell authored May 7, 2019
2 parents 3808535 + b01a3ee commit 7f78d95
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ public function find($id, $columns = ['*'])
*/
public function findMany($ids, $columns = ['*'])
{
$ids = $ids instanceof Arrayable ? $ids->toArray() : $ids;

if (empty($ids)) {
return $this->model->newCollection();
}
Expand Down
36 changes: 35 additions & 1 deletion tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ public function testFindMethod()
$this->assertEquals('baz', $result);
}

public function testFindManyMethod()
{
// ids are not empty
$builder = m::mock(Builder::class.'[get]', [$this->getMockQueryBuilder()]);
$builder->setModel($this->getMockModel());
$builder->getQuery()->shouldReceive('whereIn')->once()->with('foo_table.foo', ['one', 'two']);
$builder->shouldReceive('get')->with(['column'])->andReturn(['baz']);

$result = $builder->findMany(['one', 'two'], ['column']);
$this->assertEquals(['baz'], $result);

// ids are empty array
$builder = m::mock(Builder::class.'[get]', [$this->getMockQueryBuilder()]);
$model = $this->getMockModel();
$model->shouldReceive('newCollection')->once()->withNoArgs()->andReturn('emptycollection');
$builder->setModel($model);
$builder->getQuery()->shouldNotReceive('whereIn');
$builder->shouldNotReceive('get');

$result = $builder->findMany([], ['column']);
$this->assertEquals('emptycollection', $result);

// ids are empty collection
$builder = m::mock(Builder::class.'[get]', [$this->getMockQueryBuilder()]);
$model = $this->getMockModel();
$model->shouldReceive('newCollection')->once()->withNoArgs()->andReturn('emptycollection');
$builder->setModel($model);
$builder->getQuery()->shouldNotReceive('whereIn');
$builder->shouldNotReceive('get');

$result = $builder->findMany(collect(), ['column']);
$this->assertEquals('emptycollection', $result);
}

public function testFindOrNewMethodModelFound()
{
$model = $this->getMockModel();
Expand Down Expand Up @@ -117,7 +151,7 @@ public function testFindWithManyUsingCollection()
{
$ids = collect([1, 2]);
$builder = m::mock(Builder::class.'[get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->shouldReceive('whereIn')->once()->with('foo_table.foo', $ids);
$builder->getQuery()->shouldReceive('whereIn')->once()->with('foo_table.foo', [1, 2]);
$builder->setModel($this->getMockModel());
$builder->shouldReceive('get')->with(['column'])->andReturn('baz');

Expand Down

0 comments on commit 7f78d95

Please sign in to comment.