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 Jul 16, 2018
1 parent 02be861 commit cab365a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 57 deletions.
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ public function whereKeyNot($id)
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
if ($column instanceof Closure) {
$query = $this->model->newQueryWithoutScopes();

$column($query);
$column($query = $this->model->newModelQuery());

$this->query->addNestedWhereQuery($query->getQuery(), $boolean);
} else {
Expand Down
23 changes: 15 additions & 8 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public function push()
*/
public function save(array $options = [])
{
$query = $this->newQueryWithoutScopes();
$query = $this->newModelQuery();

// 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 @@ -811,7 +811,7 @@ public function forceDelete()
*/
protected function performDeleteOnModel()
{
$this->setKeysForSaveQuery($this->newQueryWithoutScopes())->delete();
$this->setKeysForSaveQuery($this->newModelQuery())->delete();

$this->exists = false;
}
Expand All @@ -836,6 +836,18 @@ public function newQuery()
return $this->registerGlobalScopes($this->newQueryWithoutScopes());
}

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

/**
* Get a new query builder with no relationships loaded.
*
Expand Down Expand Up @@ -870,12 +882,7 @@ public function registerGlobalScopes($builder)
*/
public function newQueryWithoutScopes()
{
$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)
return $this->newModelQuery()
->with($this->with)
->withCount($this->withCount);
}
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 @@ -47,7 +47,7 @@ protected function performDeleteOnModel()
if ($this->forceDeleting) {
$this->exists = false;

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

return $this->runSoftDelete();
Expand All @@ -60,7 +60,7 @@ protected function performDeleteOnModel()
*/
protected function runSoftDelete()
{
$query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());
$query = $this->newModelQuery()->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('newModelQuery')->once()->andReturn($nestedQuery);
$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('from');
$builder->setModel($model);
Expand Down
Loading

0 comments on commit cab365a

Please sign in to comment.