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.6] Make chunkById() work for non-incrementing/non-integer ids as well #24563

Merged
merged 1 commit into from
Jun 12, 2018
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ public function chunkById($count, callable $callback, $column = null, $alias = n

$alias = is_null($alias) ? $column : $alias;

$lastId = 0;
$lastId = null;

do {
$clone = clone $this;
Expand Down
11 changes: 7 additions & 4 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1805,16 +1805,19 @@ public function forPage($page, $perPage = 15)
* Constrain the query to the next "page" of results after a given ID.
*
* @param int $perPage
* @param int $lastId
* @param int|null $lastId
* @param string $column
* @return \Illuminate\Database\Query\Builder|static
*/
public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id')
{
$this->orders = $this->removeExistingOrdersFor($column);

return $this->where($column, '>', $lastId)
->orderBy($column, 'asc')
if ($lastId !== null) {
$this->where($column, '>', $lastId);
}

return $this->orderBy($column, 'asc')
->take($perPage);
}

Expand Down Expand Up @@ -2088,7 +2091,7 @@ public function chunkById($count, callable $callback, $column = 'id', $alias = n
{
$alias = $alias ?: $column;

$lastId = 0;
$lastId = null;

do {
$clone = clone $this;
Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,25 @@ public function testCreatingModelWithEmptyAttributes()
$this->assertFalse($model->wasRecentlyCreated);
}

public function testChunkByIdWithNonIncrementingKey()
{
EloquentTestNonIncrementingSecond::create(['name' => ' First']);
EloquentTestNonIncrementingSecond::create(['name' => ' Second']);
EloquentTestNonIncrementingSecond::create(['name' => ' Third']);

$i = 0;
EloquentTestNonIncrementingSecond::query()->chunkById(2, function (Collection $users) use (&$i) {
if (! $i) {
$this->assertEquals(' First', $users[0]->name);
$this->assertEquals(' Second', $users[1]->name);
} else {
$this->assertEquals(' Third', $users[0]->name);
}
$i++;
}, 'name');
$this->assertEquals(2, $i);
}

public function testPluck()
{
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
Expand Down Expand Up @@ -1307,6 +1326,11 @@ class EloquentTestNonIncrementing extends Eloquent
public $timestamps = false;
}

class EloquentTestNonIncrementingSecond extends EloquentTestNonIncrementing
{
protected $connection = 'second_connection';
}

class EloquentTestUserWithGlobalScope extends EloquentTestUser
{
public static function boot()
Expand Down