Skip to content

Commit 9a68bbc

Browse files
committed
cleaning up code
1 parent a8e577d commit 9a68bbc

File tree

6 files changed

+104
-28
lines changed

6 files changed

+104
-28
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+18-16
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function applyGlobalScope($identifier, $scope)
9696
* @param \Illuminate\Database\Eloquent\ScopeInterface|string $scope
9797
* @return $this
9898
*/
99-
public function removeGlobalScope($scope)
99+
public function withoutGlobalScope($scope)
100100
{
101101
if (is_string($scope)) {
102102
unset($this->scopes[$scope]);
@@ -118,7 +118,7 @@ public function removeGlobalScope($scope)
118118
*
119119
* @return $this
120120
*/
121-
public function removeGlobalScopes()
121+
public function withoutGlobalScopes()
122122
{
123123
$this->scopes = [];
124124

@@ -461,7 +461,7 @@ public function onDelete(Closure $callback)
461461
*/
462462
public function getModels($columns = ['*'])
463463
{
464-
$results = $this->getQueryWithScopes()->get($columns);
464+
$results = $this->loadScopes()->getQuery()->get($columns);
465465

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

@@ -641,7 +641,9 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C
641641
call_user_func($callback, $query);
642642
}
643643

644-
return $this->addHasWhere($query, $relation, $operator, $count, $boolean);
644+
return $this->addHasWhere(
645+
$query->loadScopes(), $relation, $operator, $count, $boolean
646+
);
645647
}
646648

647649
/**
@@ -750,7 +752,7 @@ public function orWhereHas($relation, Closure $callback, $operator = '>=', $coun
750752
*/
751753
protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator, $count, $boolean)
752754
{
753-
$this->mergeWheresToHas($hasQuery, $relation);
755+
$this->mergeModelDefinedRelationWheresToHasQuery($hasQuery, $relation);
754756

755757
if (is_numeric($count)) {
756758
$count = new Expression($count);
@@ -766,14 +768,14 @@ protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator,
766768
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation
767769
* @return void
768770
*/
769-
protected function mergeWheresToHas(Builder $hasQuery, Relation $relation)
771+
protected function mergeModelDefinedRelationWheresToHasQuery(Builder $hasQuery, Relation $relation)
770772
{
771773
// Here we have the "has" query and the original relation. We need to copy over any
772774
// where clauses the developer may have put in the relationship function over to
773775
// the has query, and then copy the bindings from the "has" query to the main.
774776
$relationQuery = $relation->getBaseQuery();
775777

776-
$hasQuery->removeGlobalScopes()->mergeWheres(
778+
$hasQuery->mergeWheres(
777779
$relationQuery->wheres, $relationQuery->getBindings()
778780
);
779781

@@ -805,7 +807,7 @@ public function with($relations)
805807
$relations = func_get_args();
806808
}
807809

808-
$eagers = $this->parseRelations($relations);
810+
$eagers = $this->parseWithRelations($relations);
809811

810812
$this->eagerLoad = array_merge($this->eagerLoad, $eagers);
811813

@@ -818,7 +820,7 @@ public function with($relations)
818820
* @param array $relations
819821
* @return array
820822
*/
821-
protected function parseRelations(array $relations)
823+
protected function parseWithRelations(array $relations)
822824
{
823825
$results = [];
824826

@@ -835,7 +837,7 @@ protected function parseRelations(array $relations)
835837
// We need to separate out any nested includes. Which allows the developers
836838
// to load deep relationships using "dots" without stating each level of
837839
// the relationship with its own key in the array of eager load names.
838-
$results = $this->parseNested($name, $results);
840+
$results = $this->parseNestedWith($name, $results);
839841

840842
$results[$name] = $constraints;
841843
}
@@ -850,7 +852,7 @@ protected function parseRelations(array $relations)
850852
* @param array $results
851853
* @return array
852854
*/
853-
protected function parseNested($name, $results)
855+
protected function parseNestedWith($name, $results)
854856
{
855857
$progress = [];
856858

@@ -885,12 +887,12 @@ protected function callScope($scope, $parameters)
885887
/**
886888
* Get the underlying query builder instance with applied global scopes.
887889
*
888-
* @return \Illuminate\Database\Query\Builder|static
890+
* @return \Illuminate\Database\Eloquent\Builder|static
889891
*/
890-
public function getQueryWithScopes()
892+
public function loadScopes()
891893
{
892894
if (! $this->scopes) {
893-
return $this->getQuery();
895+
return $this;
894896
}
895897

896898
$builder = clone $this;
@@ -905,7 +907,7 @@ public function getQueryWithScopes()
905907
}
906908
}
907909

908-
return $builder->getQuery();
910+
return $builder;
909911
}
910912

911913
/**
@@ -1022,7 +1024,7 @@ public function __call($method, $parameters)
10221024
}
10231025

10241026
if (in_array($method, $this->passthru)) {
1025-
return call_user_func_array([$this->getQueryWithScopes(), $method], $parameters);
1027+
return call_user_func_array([$this->loadScopes()->getQuery(), $method], $parameters);
10261028
}
10271029

10281030
call_user_func_array([$this->query, $method], $parameters);

src/Illuminate/Database/Eloquent/Model.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ public function newQueryWithoutScope($scope)
18691869
{
18701870
$builder = $this->newQuery();
18711871

1872-
return $builder->removeGlobalScope($scope);
1872+
return $builder->withoutGlobalScope($scope);
18731873
}
18741874

18751875
/**

src/Illuminate/Database/Eloquent/SoftDeletingScope.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected function addRestore(Builder $builder)
9898
protected function addWithTrashed(Builder $builder)
9999
{
100100
$builder->macro('withTrashed', function (Builder $builder) {
101-
return $builder->removeGlobalScope($this);
101+
return $builder->withoutGlobalScope($this);
102102
});
103103
}
104104

@@ -113,7 +113,9 @@ protected function addOnlyTrashed(Builder $builder)
113113
$builder->macro('onlyTrashed', function (Builder $builder) {
114114
$model = $builder->getModel();
115115

116-
$builder->removeGlobalScope($this)->getQuery()->whereNotNull($model->getQualifiedDeletedAtColumn());
116+
$builder->withoutGlobalScope($this)->whereNotNull(
117+
$model->getQualifiedDeletedAtColumn()
118+
);
117119

118120
return $builder;
119121
});

tests/Database/DatabaseEloquentGlobalScopesTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testGlobalScopeIsApplied()
2020
public function testGlobalScopeCanBeRemoved()
2121
{
2222
$model = new EloquentGlobalScopesTestModel();
23-
$query = $model->newQuery()->removeGlobalScope(ActiveScope::class);
23+
$query = $model->newQuery()->withoutGlobalScope(ActiveScope::class);
2424
$this->assertEquals('select * from "table"', $query->toSql());
2525
$this->assertEquals([], $query->getBindings());
2626
}
@@ -36,7 +36,7 @@ public function testClosureGlobalScopeIsApplied()
3636
public function testClosureGlobalScopeCanBeRemoved()
3737
{
3838
$model = new EloquentClosureGlobalScopesTestModel();
39-
$query = $model->newQuery()->removeGlobalScope('active_scope');
39+
$query = $model->newQuery()->withoutGlobalScope('active_scope');
4040
$this->assertEquals('select * from "table" order by "name" asc', $query->toSql());
4141
$this->assertEquals([], $query->getBindings());
4242
}
@@ -48,19 +48,19 @@ public function testGlobalScopeCanBeRemovedAfterTheQueryIsExecuted()
4848
$this->assertEquals('select * from "table" where "active" = ? order by "name" asc', $query->toSql());
4949
$this->assertEquals([1], $query->getBindings());
5050

51-
$query->removeGlobalScope('active_scope');
51+
$query->withoutGlobalScope('active_scope');
5252
$this->assertEquals('select * from "table" order by "name" asc', $query->toSql());
5353
$this->assertEquals([], $query->getBindings());
5454
}
5555

5656
public function testAllGlobalScopesCanBeRemoved()
5757
{
5858
$model = new EloquentClosureGlobalScopesTestModel();
59-
$query = $model->newQuery()->removeGlobalScopes();
59+
$query = $model->newQuery()->withoutGlobalScopes();
6060
$this->assertEquals('select * from "table"', $query->toSql());
6161
$this->assertEquals([], $query->getBindings());
6262

63-
$query = EloquentClosureGlobalScopesTestModel::removeGlobalScopes();
63+
$query = EloquentClosureGlobalScopesTestModel::withoutGlobalScopes();
6464
$this->assertEquals('select * from "table"', $query->toSql());
6565
$this->assertEquals([], $query->getBindings());
6666
}

tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php

+73-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public function createSchema()
4444
$table->timestamps();
4545
$table->softDeletes();
4646
});
47+
48+
$this->schema()->create('comments', function ($table) {
49+
$table->increments('id');
50+
$table->integer('post_id');
51+
$table->string('body');
52+
$table->timestamps();
53+
$table->softDeletes();
54+
});
4755
}
4856

4957
/**
@@ -54,6 +62,8 @@ public function createSchema()
5462
public function tearDown()
5563
{
5664
$this->schema()->drop('users');
65+
$this->schema()->drop('posts');
66+
$this->schema()->drop('comments');
5767
}
5868

5969
/**
@@ -137,11 +147,55 @@ public function testWhereHasWithDeletedRelationship()
137147

138148
$abigail = SoftDeletesTestUser::where('email', '[email protected]')->first();
139149
$post = $abigail->posts()->create(['title' => 'First Title']);
140-
$post->delete();
141150

151+
$users = SoftDeletesTestUser::where('email', '[email protected]')->has('posts')->get();
152+
$this->assertEquals(0, count($users));
153+
154+
$users = SoftDeletesTestUser::where('email', '[email protected]')->has('posts')->get();
155+
$this->assertEquals(1, count($users));
156+
157+
$users = SoftDeletesTestUser::where('email', '[email protected]')->orHas('posts')->get();
158+
$this->assertEquals(1, count($users));
159+
160+
$users = SoftDeletesTestUser::whereHas('posts', function ($query) {
161+
$query->where('title', 'First Title');
162+
})->get();
163+
$this->assertEquals(1, count($users));
164+
165+
$users = SoftDeletesTestUser::whereHas('posts', function ($query) {
166+
$query->where('title', 'Another Title');
167+
})->get();
168+
$this->assertEquals(0, count($users));
169+
170+
$users = SoftDeletesTestUser::where('email', '[email protected]')->orWhereHas('posts', function ($query) {
171+
$query->where('title', 'First Title');
172+
})->get();
173+
$this->assertEquals(1, count($users));
174+
175+
// With Post Deleted...
176+
177+
$post->delete();
142178
$users = SoftDeletesTestUser::has('posts')->get();
179+
$this->assertEquals(0, count($users));
180+
}
143181

182+
/**
183+
* @group test
184+
*/
185+
public function testWhereHasWithNestedDeletedRelationship()
186+
{
187+
$this->createUsers();
188+
189+
$abigail = SoftDeletesTestUser::where('email', '[email protected]')->first();
190+
$post = $abigail->posts()->create(['title' => 'First Title']);
191+
$comment = $post->comments()->create(['body' => 'Comment Body']);
192+
$comment->delete();
193+
194+
$users = SoftDeletesTestUser::has('posts.comments')->get();
144195
$this->assertEquals(0, count($users));
196+
197+
$users = SoftDeletesTestUser::doesntHave('posts.comments')->get();
198+
$this->assertEquals(1, count($users));
145199
}
146200

147201
/**
@@ -203,4 +257,22 @@ class SoftDeletesTestPost extends Eloquent
203257
protected $dates = ['deleted_at'];
204258
protected $table = 'posts';
205259
protected $guarded = [];
260+
261+
public function comments()
262+
{
263+
return $this->hasMany(SoftDeletesTestComment::class, 'post_id');
264+
}
265+
}
266+
267+
268+
/**
269+
* Eloquent Models...
270+
*/
271+
class SoftDeletesTestComment extends Eloquent
272+
{
273+
use SoftDeletes;
274+
275+
protected $dates = ['deleted_at'];
276+
protected $table = 'comments';
277+
protected $guarded = [];
206278
}

tests/Database/DatabaseSoftDeletingScopeTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testWithTrashedExtension()
6060
$callback = $builder->getMacro('withTrashed');
6161
$givenBuilder = m::mock('Illuminate\Database\Eloquent\Builder');
6262
$givenBuilder->shouldReceive('getModel')->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
63-
$givenBuilder->shouldReceive('removeGlobalScope')->with($scope)->andReturn($givenBuilder);
63+
$givenBuilder->shouldReceive('withoutGlobalScope')->with($scope)->andReturn($givenBuilder);
6464
$result = $callback($givenBuilder);
6565

6666
$this->assertEquals($givenBuilder, $result);
@@ -78,9 +78,9 @@ public function testOnlyTrashedExtension()
7878
$givenBuilder = m::mock('Illuminate\Database\Eloquent\Builder');
7979
$givenBuilder->shouldReceive('getQuery')->andReturn($query = m::mock('StdClass'));
8080
$givenBuilder->shouldReceive('getModel')->andReturn($model);
81-
$givenBuilder->shouldReceive('removeGlobalScope')->with($scope)->andReturn($givenBuilder);
81+
$givenBuilder->shouldReceive('withoutGlobalScope')->with($scope)->andReturn($givenBuilder);
8282
$model->shouldReceive('getQualifiedDeletedAtColumn')->andReturn('table.deleted_at');
83-
$query->shouldReceive('whereNotNull')->once()->with('table.deleted_at');
83+
$givenBuilder->shouldReceive('whereNotNull')->once()->with('table.deleted_at');
8484
$result = $callback($givenBuilder);
8585

8686
$this->assertEquals($givenBuilder, $result);

0 commit comments

Comments
 (0)