Skip to content

Fix issue with chunkById. #1317

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

Merged
merged 2 commits into from
Nov 3, 2017
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
8 changes: 8 additions & 0 deletions src/Jenssegers/Mongodb/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ public function decrement($column, $amount = 1, array $extra = [])
return parent::decrement($column, $amount, $extra);
}

/**
* @inheritdoc
*/
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
{
return parent::chunkById($count, $callback, $column, $alias);
}

/**
* @inheritdoc
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,28 @@ public function decrement($column, $amount = 1, array $extra = [], array $option
return $this->increment($column, -1 * $amount, $extra, $options);
}

/**
* @inheritdoc
*/
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
{
return parent::chunkById($count, $callback, $column, $alias);
}

/**
* @inheritdoc
*/
public function forPageAfterId($perPage = 15, $lastId = 0, $column = '_id')
{
// When using ObjectIDs to paginate, we need to use a hex string as the
// "minimum" ID rather than the integer zero so the '$lt' query works.
if ($column === '_id' && $lastId === 0) {
$lastId = '000000000000000000000000';
}

return parent::forPageAfterId($perPage, $lastId, $column);
}

/**
* @inheritdoc
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,18 @@ public function testGetDirtyDates()
$user->birthday = new DateTime('19 august 1989');
$this->assertEmpty($user->getDirty());
}

public function testChunkById()
{
User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]);
User::create(['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']]);
User::create(['name' => 'spoon', 'tags' => ['round', 'bowl']]);

$count = 0;
User::chunkById(2, function ($items) use (&$count) {
$count += count($items);
});

$this->assertEquals(3, $count);
}
}