Skip to content

Commit 5478822

Browse files
authored
Apply withoutGlobalScope in CanBeOneOfMany subqueries (#39295)
1 parent dade8df commit 5478822

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ protected function getDefaultOneOfManyJoinAlias($relation)
181181
protected function newOneOfManySubQuery($groupBy, $column = null, $aggregate = null)
182182
{
183183
$subQuery = $this->query->getModel()
184-
->newQuery();
184+
->newQuery()
185+
->withoutGlobalScopes($this->removedScopes());
185186

186187
foreach (Arr::wrap($groupBy) as $group) {
187188
$subQuery->groupBy($this->qualifyRelatedColumn($group));

tests/Database/DatabaseEloquentHasOneOfManyTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,29 @@ public function testEagerLoadingAppliesConstraintsToInnerJoinSubQuery()
110110
$this->assertSame('select MAX("id") as "id_aggregate", "logins"."user_id" from "logins" where "logins"."user_id" = ? and "logins"."user_id" is not null and "logins"."user_id" in (1) group by "logins"."user_id"', $relation->getOneOfManySubQuery()->toSql());
111111
}
112112

113+
public function testGlobalScopeIsNotAppliedWhenRelationIsDefinedWithoutGlobalScope()
114+
{
115+
HasOneOfManyTestLogin::addGlobalScope(function ($query) {
116+
$query->orderBy('id');
117+
});
118+
119+
$user = HasOneOfManyTestUser::create();
120+
$relation = $user->latest_login_without_global_scope();
121+
$relation->addEagerConstraints([$user]);
122+
$this->assertSame('select * from "logins" inner join (select MAX("id") as "id_aggregate", "logins"."user_id" from "logins" where "logins"."user_id" = ? and "logins"."user_id" is not null and "logins"."user_id" in (1) group by "logins"."user_id") as "latestOfMany" on "latestOfMany"."id_aggregate" = "logins"."id" and "latestOfMany"."user_id" = "logins"."user_id" where "logins"."user_id" = ? and "logins"."user_id" is not null', $relation->getQuery()->toSql());
123+
}
124+
125+
public function testGlobalScopeIsNotAppliedWhenRelationIsDefinedWithoutGlobalScopeWithComplexQuery()
126+
{
127+
HasOneOfManyTestPrice::addGlobalScope(function ($query) {
128+
$query->orderBy('id');
129+
});
130+
131+
$user = HasOneOfManyTestUser::create();
132+
$relation = $user->price_without_global_scope();
133+
$this->assertSame('select * from "prices" inner join (select max("id") as "id_aggregate", "prices"."user_id" from "prices" inner join (select max("published_at") as "published_at_aggregate", "prices"."user_id" from "prices" where "published_at" < ? and "prices"."user_id" = ? and "prices"."user_id" is not null group by "prices"."user_id") as "price_without_global_scope" on "price_without_global_scope"."published_at_aggregate" = "prices"."published_at" and "price_without_global_scope"."user_id" = "prices"."user_id" where "published_at" < ? group by "prices"."user_id") as "price_without_global_scope" on "price_without_global_scope"."id_aggregate" = "prices"."id" and "price_without_global_scope"."user_id" = "prices"."user_id" where "prices"."user_id" = ? and "prices"."user_id" is not null', $relation->getQuery()->toSql());
134+
}
135+
113136
public function testQualifyingSubSelectColumn()
114137
{
115138
$user = HasOneOfManyTestUser::create();
@@ -468,6 +491,11 @@ public function latest_login_with_invalid_aggregate()
468491
return $this->hasOne(HasOneOfManyTestLogin::class, 'user_id')->ofMany('id', 'count');
469492
}
470493

494+
public function latest_login_without_global_scope()
495+
{
496+
return $this->hasOne(HasOneOfManyTestLogin::class, 'user_id')->withoutGlobalScopes()->latestOfMany();
497+
}
498+
471499
public function first_login()
472500
{
473501
return $this->hasOne(HasOneOfManyTestLogin::class, 'user_id')->ofMany('id', 'min');
@@ -522,6 +550,16 @@ public function price_with_shortcut()
522550
{
523551
return $this->hasOne(HasOneOfManyTestPrice::class, 'user_id')->latestOfMany(['published_at', 'id']);
524552
}
553+
554+
public function price_without_global_scope()
555+
{
556+
return $this->hasOne(HasOneOfManyTestPrice::class, 'user_id')->withoutGlobalScopes()->ofMany([
557+
'published_at' => 'max',
558+
'id' => 'max',
559+
], function ($q) {
560+
$q->where('published_at', '<', now());
561+
});
562+
}
525563
}
526564

527565
class HasOneOfManyTestModel extends Eloquent

0 commit comments

Comments
 (0)