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.6] Fix $withCount binding problems #24240

Merged
merged 1 commit into from
May 21, 2018
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function whereKeyNot($id)
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
if ($column instanceof Closure) {
$query = $this->model->newQueryWithoutScopes();
$query = $this->model->newUneagerQueryWithoutScopes();

$column($query);

Expand Down
20 changes: 15 additions & 5 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public function push()
*/
public function save(array $options = [])
{
$query = $this->newQueryWithoutScopes();
$query = $this->newUneagerQueryWithoutScopes();

// If the "saving" event returns false we'll bail out of the save and return
// false, indicating that the save failed. This provides a chance for any
Expand Down Expand Up @@ -815,7 +815,7 @@ public function forceDelete()
*/
protected function performDeleteOnModel()
{
$this->setKeysForSaveQuery($this->newQueryWithoutScopes())->delete();
$this->setKeysForSaveQuery($this->newUneagerQueryWithoutScopes())->delete();

$this->exists = false;
}
Expand Down Expand Up @@ -873,15 +873,25 @@ public function registerGlobalScopes($builder)
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function newQueryWithoutScopes()
{
return $this->newUneagerQueryWithoutScopes()
->with($this->with)
->withCount($this->withCount);
}

/**
* Get a new query builder that doesn't have any global scopes or eager loading.
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function newUneagerQueryWithoutScopes()
{
$builder = $this->newEloquentBuilder($this->newBaseQueryBuilder());

// Once we have the query builders, we will set the model instances so the
// builder can easily access any information it may need from the model
// while it is constructing and executing various queries against it.
return $builder->setModel($this)
->with($this->with)
->withCount($this->withCount);
return $builder->setModel($this);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function performDeleteOnModel()
if ($this->forceDeleting) {
$this->exists = false;

return $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey())->forceDelete();
return $this->newUneagerQueryWithoutScopes()->where($this->getKeyName(), $this->getKey())->forceDelete();
}

return $this->runSoftDelete();
Expand All @@ -62,7 +62,7 @@ protected function performDeleteOnModel()
*/
protected function runSoftDelete()
{
$query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());
$query = $this->newUneagerQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());

$time = $this->freshTimestamp();

Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public function testNestedWhere()
$nestedRawQuery = $this->getMockQueryBuilder();
$nestedQuery->shouldReceive('getQuery')->once()->andReturn($nestedRawQuery);
$model = $this->getMockModel()->makePartial();
$model->shouldReceive('newQueryWithoutScopes')->once()->andReturn($nestedQuery);
$model->shouldReceive('newUneagerQueryWithoutScopes')->once()->andReturn($nestedQuery);
$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('from');
$builder->setModel($model);
Expand Down
Loading