Skip to content

Commit a6ef21a

Browse files
[8.x] Only select related columns by default in CanBeOneOfMany::ofMany (#39307)
* Only select related columns by default in CanBeOneOfMany::ofMany * Update CanBeOneOfMany.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent cf9726d commit a6ef21a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null)
124124

125125
$this->addConstraints();
126126

127+
$columns = $this->query->getQuery()->columns;
128+
129+
if (is_null($columns) || $columns === ['*']) {
130+
$this->select([$this->qualifyColumn('*')]);
131+
}
132+
127133
return $this;
128134
}
129135

tests/Database/DatabaseEloquentHasOneOfManyTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testGlobalScopeIsNotAppliedWhenRelationIsDefinedWithoutGlobalSco
119119
$user = HasOneOfManyTestUser::create();
120120
$relation = $user->latest_login_without_global_scope();
121121
$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());
122+
$this->assertSame('select "logins".* 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());
123123
}
124124

125125
public function testGlobalScopeIsNotAppliedWhenRelationIsDefinedWithoutGlobalScopeWithComplexQuery()
@@ -130,7 +130,7 @@ public function testGlobalScopeIsNotAppliedWhenRelationIsDefinedWithoutGlobalSco
130130

131131
$user = HasOneOfManyTestUser::create();
132132
$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());
133+
$this->assertSame('select "prices".* 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());
134134
}
135135

136136
public function testQualifyingSubSelectColumn()
@@ -158,6 +158,16 @@ public function testItGetsCorrectResults()
158158
$this->assertSame($latestLogin->id, $result->id);
159159
}
160160

161+
public function testResultDoesNotHaveAggregateColumn()
162+
{
163+
$user = HasOneOfManyTestUser::create();
164+
$user->logins()->create();
165+
166+
$result = $user->latest_login()->getResults();
167+
$this->assertNotNull($result);
168+
$this->assertFalse(isset($result->id_aggregate));
169+
}
170+
161171
public function testItGetsCorrectResultsUsingShortcutMethod()
162172
{
163173
$user = HasOneOfManyTestUser::create();

0 commit comments

Comments
 (0)