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.2] Fix each when there's no order by #11623

Merged
merged 2 commits into from Dec 31, 2015
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
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ public function chunk($count, callable $callback)
*/
public function each(callable $callback, $count = 1000)
{
if (is_null($this->getOrderBys())) {
$this->orderBy($this->model->getQualifiedKeyName(), 'asc');
}

return $this->chunk($count, function ($results) use ($callback) {
foreach ($results as $key => $value) {
if ($callback($item, $key) === false) {
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Query;

use Closure;
use RuntimeException;
use BadMethodCallException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -1572,9 +1573,15 @@ public function chunk($count, callable $callback)
* @param callable $callback
* @param int $count
* @return bool
*
* @throws \RuntimeException
*/
public function each(callable $callback, $count = 1000)
{
if (is_null($this->getOrderBys())) {
throw new RuntimeException('You must provided an ordering on the query.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be "provide"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, oops. Thanks. ;)

}

return $this->chunk($count, function ($results) use ($callback) {
foreach ($results as $key => $value) {
if ($callback($item, $key) === false) {
Expand All @@ -1584,6 +1591,18 @@ public function each(callable $callback, $count = 1000)
});
}

/**
* Returns the currently set ordering.
*
* @return array|null
*/
public function getOrderBys()
{
$property = $this->unions ? 'unionOrders' : 'orders';

return $this->{$property};
}

/**
* Get an array with the values of a given column.
*
Expand Down