Skip to content

Commit

Permalink
Merge pull request #9679 from dtaub/count-bindings-fix
Browse files Browse the repository at this point in the history
[5.1] Fix getCountForPagination backup/restore bindings.
  • Loading branch information
taylorotwell committed Jul 30, 2015
2 parents c9c74ac + b015ef9 commit 4c48912
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ class Builder
*/
protected $backups = [];

/**
* The binding backups currently in use.
*
* @var array
*/
protected $bindingBackups = [];

/**
* All of the available clause operators.
*
Expand Down Expand Up @@ -1463,6 +1470,12 @@ protected function backupFieldsForCount()

$this->{$field} = null;
}

foreach (['order', 'select'] as $key) {
$this->bindingBackups[$key] = $this->bindings[$key];

$this->bindings[$key] = [];
}
}

/**
Expand All @@ -1476,7 +1489,12 @@ protected function restoreFieldsForCount()
$this->{$field} = $this->backups[$field];
}

foreach (['order', 'select'] as $key) {
$this->bindings[$key] = $this->bindingBackups[$key];
}

$this->backups = [];
$this->bindingBackups = [];
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,19 @@ public function testLimitsAndOffsets()
$this->assertEquals('select * from "users" limit 15 offset 0', $builder->toSql());
}

public function testGetCountForPaginationWithBindings()
{
$builder = $this->getBuilder();
$builder->from('users')->selectSub(function($q) { $q->select('body')->from('posts')->where('id', 4); }, 'post');

$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; });

$count = $builder->getCountForPagination();
$this->assertEquals(1, $count);
$this->assertEquals([4], $builder->getBindings());
}

public function testWhereShortcut()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 4c48912

Please sign in to comment.