Skip to content

Commit 5454ac9

Browse files
authored
Merge pull request #1317 from DoSomething/chunkById-5.5
Fix issue with chunkById.
2 parents cf15156 + 6261109 commit 5454ac9

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ public function decrement($column, $amount = 1, array $extra = [])
143143
return parent::decrement($column, $amount, $extra);
144144
}
145145

146+
/**
147+
* @inheritdoc
148+
*/
149+
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
150+
{
151+
return parent::chunkById($count, $callback, $column, $alias);
152+
}
153+
146154
/**
147155
* @inheritdoc
148156
*/

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,28 @@ public function decrement($column, $amount = 1, array $extra = [], array $option
618618
return $this->increment($column, -1 * $amount, $extra, $options);
619619
}
620620

621+
/**
622+
* @inheritdoc
623+
*/
624+
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
625+
{
626+
return parent::chunkById($count, $callback, $column, $alias);
627+
}
628+
629+
/**
630+
* @inheritdoc
631+
*/
632+
public function forPageAfterId($perPage = 15, $lastId = 0, $column = '_id')
633+
{
634+
// When using ObjectIDs to paginate, we need to use a hex string as the
635+
// "minimum" ID rather than the integer zero so the '$lt' query works.
636+
if ($column === '_id' && $lastId === 0) {
637+
$lastId = '000000000000000000000000';
638+
}
639+
640+
return parent::forPageAfterId($perPage, $lastId, $column);
641+
}
642+
621643
/**
622644
* @inheritdoc
623645
*/

tests/ModelTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,18 @@ public function testGetDirtyDates()
534534
$user->birthday = new DateTime('19 august 1989');
535535
$this->assertEmpty($user->getDirty());
536536
}
537+
538+
public function testChunkById()
539+
{
540+
User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]);
541+
User::create(['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']]);
542+
User::create(['name' => 'spoon', 'tags' => ['round', 'bowl']]);
543+
544+
$count = 0;
545+
User::chunkById(2, function ($items) use (&$count) {
546+
$count += count($items);
547+
});
548+
549+
$this->assertEquals(3, $count);
550+
}
537551
}

0 commit comments

Comments
 (0)