Skip to content

Commit

Permalink
more scope cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 2, 2015
1 parent 9a68bbc commit ca15fe7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(QueryBuilder $query)
* @param \Illuminate\Database\Eloquent\ScopeInterface|\Closure $scope
* @return $this
*/
public function applyGlobalScope($identifier, $scope)
public function withGlobalScope($identifier, $scope)
{
$this->scopes[$identifier] = $scope;

Expand Down Expand Up @@ -461,7 +461,7 @@ public function onDelete(Closure $callback)
*/
public function getModels($columns = ['*'])
{
$results = $this->loadScopes()->getQuery()->get($columns);
$results = $this->applyScopes()->getQuery()->get($columns);

$connection = $this->model->getConnectionName();

Expand Down Expand Up @@ -642,7 +642,7 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C
}

return $this->addHasWhere(
$query->loadScopes(), $relation, $operator, $count, $boolean
$query->applyScopes(), $relation, $operator, $count, $boolean
);
}

Expand Down Expand Up @@ -885,11 +885,11 @@ protected function callScope($scope, $parameters)
}

/**
* Get the underlying query builder instance with applied global scopes.
* Apply the scopes to the Eloquent builder instance and return it.
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function loadScopes()
public function applyScopes()
{
if (! $this->scopes) {
return $this;
Expand Down Expand Up @@ -1024,7 +1024,7 @@ public function __call($method, $parameters)
}

if (in_array($method, $this->passthru)) {
return call_user_func_array([$this->loadScopes()->getQuery(), $method], $parameters);
return call_user_func_array([$this->applyScopes()->getQuery(), $method], $parameters);
}

call_user_func_array([$this->query, $method], $parameters);
Expand Down
21 changes: 5 additions & 16 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,11 @@ public function newQuery()
{
$builder = $this->newQueryWithoutScopes();

return $this->applyGlobalScopes($builder);
foreach ($this->getGlobalScopes() as $identifier => $scope) {
$builder->withGlobalScope($identifier, $scope);
}

return $builder;
}

/**
Expand Down Expand Up @@ -1889,21 +1893,6 @@ public function newQueryWithoutScopes()
return $builder->setModel($this)->with($this->with);
}

/**
* Apply all of the global scopes to an Eloquent builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function applyGlobalScopes($builder)
{
foreach ($this->getGlobalScopes() as $identifier => $scope) {
$builder->applyGlobalScope($identifier, $scope);
}

return $builder;
}

/**
* Create a new Eloquent query builder for the model.
*
Expand Down
3 changes: 0 additions & 3 deletions tests/Database/DatabaseEloquentRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ public function testDonNotRunParentModelGlobalScopes()
$grammar->shouldReceive('wrap');
$parent->shouldReceive('newQueryWithoutScopes')->andReturn($eloquentBuilder);

//Test Condition
$parent->shouldReceive('applyGlobalScopes')->andReturn($eloquentBuilder)->never();

$relation = new EloquentRelationStub($eloquentBuilder, $parent);
$relation->wrap('test');
}
Expand Down

0 comments on commit ca15fe7

Please sign in to comment.