Skip to content

[5.9] EachById #28065

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 1 commit into from
Apr 1, 2019
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
66 changes: 66 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,72 @@ public function each(callable $callback, $count = 1000)
});
}

/**
* Chunk the results of a query by comparing IDs.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @return bool
*/
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
$column = $column ?? $this->defaultKeyName();

$alias = $alias ?? $column;

$lastId = null;

do {
$clone = clone $this;

// We'll execute the query for the given page and get the results. If there are
// no results we can just break and return from here. When there are results
// we will call the callback with the current chunk of these results here.
$results = $clone->forPageAfterId($count, $lastId, $column)->get();

$countResults = $results->count();

if ($countResults == 0) {
break;
}

// On each chunk result set, we will pass them to the callback and then let the
// developer take care of everything within the callback, which allows us to
// keep the memory low for spinning through large result sets for working.
if ($callback($results) === false) {
return false;
}

$lastId = $results->last()->{$alias};

unset($results);
} while ($countResults == $count);

return true;
}

/**
* Execute a callback over each item while chunking by id.
*
* @param callable $callback
* @param int $count
* @param string $column
* @param string $alias
* @return bool
*/
public function eachById(callable $callback, $count = 1000, $column = null, $alias = null)
{
return $this->chunkById($count, function ($results) use ($callback) {
foreach ($results as $key => $value) {
if ($callback($value, $key) === false) {
return false;
}
}
}, $column, $alias);
}

/**
* Execute the query and get the first result.
*
Expand Down
44 changes: 4 additions & 40 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,49 +646,13 @@ public function cursor()
}

/**
* Chunk the results of a query by comparing numeric IDs.
* Get the default key name of the table.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @return bool
* @return string
*/
public function chunkById($count, callable $callback, $column = null, $alias = null)
protected function defaultKeyName()
{
$column = is_null($column) ? $this->getModel()->getKeyName() : $column;

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

$lastId = null;

do {
$clone = clone $this;

// We'll execute the query for the given page and get the results. If there are
// no results we can just break and return from here. When there are results
// we will call the callback with the current chunk of these results here.
$results = $clone->forPageAfterId($count, $lastId, $column)->get();

$countResults = $results->count();

if ($countResults == 0) {
break;
}

// On each chunk result set, we will pass them to the callback and then let the
// developer take care of everything within the callback, which allows us to
// keep the memory low for spinning through large result sets for working.
if ($callback($results) === false) {
return false;
}

$lastId = $results->last()->{$alias};

unset($results);
} while ($countResults == $count);

return true;
return $this->getModel()->getKeyName();
}

/**
Expand Down
42 changes: 4 additions & 38 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2173,47 +2173,13 @@ public function cursor()
}

/**
* Chunk the results of a query by comparing numeric IDs.
* Get the default key name of the table.
*
* @param int $count
* @param callable $callback
* @param string $column
* @param string|null $alias
* @return bool
* @return string
*/
public function chunkById($count, callable $callback, $column = 'id', $alias = null)
protected function defaultKeyName()
{
$alias = $alias ?: $column;

$lastId = null;

do {
$clone = clone $this;

// We'll execute the query for the given page and get the results. If there are
// no results we can just break and return from here. When there are results
// we will call the callback with the current chunk of these results here.
$results = $clone->forPageAfterId($count, $lastId, $column)->get();

$countResults = $results->count();

if ($countResults == 0) {
break;
}

// On each chunk result set, we will pass them to the callback and then let the
// developer take care of everything within the callback, which allows us to
// keep the memory low for spinning through large result sets for working.
if ($callback($results) === false) {
return false;
}

$lastId = $results->last()->{$alias};

unset($results);
} while ($countResults == $count);

return true;
return 'id';
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,20 @@ public function testChunkByIdWithNonIncrementingKey()
$this->assertEquals(2, $i);
}

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

$users = [];
EloquentTestNonIncrementingSecond::query()->eachById(
function (EloquentTestNonIncrementingSecond $user, $i) use (&$users) {
$users[] = [$user->name, $i];
}, 2, 'name');
$this->assertSame([[' First', 0], [' Second', 1], [' Third', 0]], $users);
}

public function testPluck()
{
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
Expand Down