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

Error with chunk() #12

Open
FerdivdWoerd opened this issue Sep 6, 2018 · 0 comments
Open

Error with chunk() #12

FerdivdWoerd opened this issue Sep 6, 2018 · 0 comments

Comments

@FerdivdWoerd
Copy link

FerdivdWoerd commented Sep 6, 2018

I found a bug when using chunk on an Eloquent model instead of get.

Laravel calls a function enforceOrderBy that orders the query based on the primary key.

/**
  * Add a generic "order by" clause if the query doesn't already have one.
  *
  * @return void
  */
 protected function enforceOrderBy()
 {
     if (empty($this->query->orders) && empty($this->query->unionOrders)) {
         $this->orderBy($this->model->getQualifiedKeyName(), 'asc');
     }
 }

/**
  * Get the table qualified key name.
  *
  * @return string
  */
 public function getQualifiedKeyName()
 {
     return $this->qualifyColumn($this->getKeyName());
 }

 /**
  * Qualify the given column name by the model's table.
  *
  * @param  string  $column
  * @return string
  */
 public function qualifyColumn($column)
 {
     if (Str::contains($column, '.')) {
         return $column;
     }
 
     return $this->getTable().'.'.$column;
 }

You can circumvent this by adding your own orderBy but perhaps you could override the getQualifiedKeyName, qualifyColumn or the getKeyName function to prevent this from being necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant