From 10932f8e24c302edca71225af755b4e7d75160ee Mon Sep 17 00:00:00 2001 From: Hamid Alaei V Date: Tue, 12 Jun 2018 16:44:29 +0430 Subject: [PATCH] make chunkById() work for non-incrementing/non-integer ids --- src/Illuminate/Database/Eloquent/Builder.php | 2 +- src/Illuminate/Database/Query/Builder.php | 11 +++++---- .../DatabaseEloquentIntegrationTest.php | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index fd18e05d2a0c..2e5466b3adf3 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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; diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index ea83b59c6f56..8a1978809815 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -1805,7 +1805,7 @@ 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 */ @@ -1813,8 +1813,11 @@ 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); } @@ -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; diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index fbcb4c15eb3d..b1340663df86 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -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']); @@ -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()