Skip to content

Commit

Permalink
Fix $withCount binding problems
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed May 17, 2018
1 parent eddc5a1 commit 8c37b0d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 52 deletions.
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

0 comments on commit 8c37b0d

Please sign in to comment.