Skip to content

Commit 0d53a8c

Browse files
committed
[graphql] @sortBy: Fixed relations where (it will be added into join).
1 parent 3e293ce commit 0d53a8c

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

src/SortBy/SortBuilder.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,21 @@ protected function processRelation(
144144
if (!$stack->hasTableAlias()) {
145145
$alias = $relation->getRelationCountHash();
146146
$stack = $stack->setTableAlias($alias);
147-
$table = $relation->newModelInstance()->getTable();
148147

149148
if ($relation instanceof BelongsTo) {
150-
$builder = $builder->leftJoin(
151-
"{$table} as {$alias}",
149+
$builder = $builder->leftJoinSub(
150+
$relation->getQuery(),
151+
$alias,
152152
"{$alias}.{$relation->getOwnerKeyName()}",
153153
'=',
154154
$parentAlias
155155
? "{$parentAlias}.{$relation->getForeignKeyName()}"
156156
: $relation->getQualifiedForeignKeyName(),
157157
);
158158
} elseif ($relation instanceof HasOne) {
159-
$builder = $builder->leftJoin(
160-
"{$table} as {$alias}",
159+
$builder = $builder->leftJoinSub(
160+
$relation->getQuery(),
161+
$alias,
161162
"{$alias}.{$relation->getForeignKeyName()}",
162163
'=',
163164
$parentAlias

src/SortBy/SortBuilderTest.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,21 @@ static function (): EloquentBuilder {
188188
' "table_alias_1"."name" as "table_alias_1_name",'.
189189
' "table_alias_1"."created_at" as "table_alias_1_created_at" '.
190190
'from "table_a" '.
191-
'left join "table_b" as "table_alias_0"'.
192-
' on "table_alias_0"."id" = "table_a"."belongs_to_b_id" '.
193-
'left join "table_c" as "table_alias_1"'.
194-
' on "table_alias_1"."id" = "table_alias_0"."belongs_to_c_id" '.
191+
'left join (select * from "table_b" where "table_b"."id" = ? and "a" = ?)'.
192+
' as "table_alias_0" on "table_alias_0"."id" = "table_a"."belongs_to_b_id" '.
193+
'left join (select * from "table_c" where "table_c"."id" = ?)'.
194+
' as "table_alias_1" on "table_alias_1"."id" = "table_alias_0"."belongs_to_c_id" '.
195195
'order by'.
196196
' "table_alias_0_name" asc,'.
197197
' "table_alias_0_created_at" desc,'.
198198
' "table_alias_1_name" desc,'.
199199
' "table_alias_1_created_at" desc,'.
200200
' "table_a"."name" asc',
201-
'bindings' => [],
201+
'bindings' => [
202+
34,
203+
'a',
204+
78,
205+
],
202206
],
203207
static function (): EloquentBuilder {
204208
return SortBuilderTest__ModelA::query();
@@ -235,17 +239,23 @@ static function (): EloquentBuilder {
235239
' "table_alias_1"."name" as "table_alias_1_name",'.
236240
' "table_alias_1"."created_at" as "table_alias_1_created_at" '.
237241
'from "table_a" '.
238-
'left join "table_b" as "table_alias_0"'.
239-
' on "table_alias_0"."model_a_id" = "table_a"."id" '.
240-
'left join "table_c" as "table_alias_1"'.
241-
' on "table_alias_1"."model_b_id" = "table_alias_0"."id" '.
242+
'left join (select * from "table_b" where'.
243+
' "table_b"."model_a_id" = ? and "table_b"."model_a_id" is not null and "b" = ?'.
244+
') as "table_alias_0" on "table_alias_0"."model_a_id" = "table_a"."id" '.
245+
'left join (select * from "table_c" where'.
246+
' "table_c"."model_b_id" = ? and "table_c"."model_b_id" is not null'.
247+
') as "table_alias_1" on "table_alias_1"."model_b_id" = "table_alias_0"."id" '.
242248
'order by'.
243249
' "table_alias_0_name" asc,'.
244250
' "table_alias_0_created_at" desc,'.
245251
' "table_alias_1_name" desc,'.
246252
' "table_alias_1_created_at" desc,'.
247253
' "table_a"."name" asc',
248-
'bindings' => [],
254+
'bindings' => [
255+
12,
256+
'b',
257+
56,
258+
],
249259
],
250260
static function (): EloquentBuilder {
251261
return SortBuilderTest__ModelA::query();
@@ -292,12 +302,24 @@ class SortBuilderTest__ModelA extends Model {
292302
*/
293303
public $table = 'table_a';
294304

305+
public function __construct() {
306+
self::unguard(true);
307+
parent::__construct([
308+
$this->getKeyName() => 12,
309+
'belongs_to_b_id' => 34,
310+
]);
311+
}
312+
295313
public function belongsToB(): BelongsTo {
296-
return $this->belongsTo(SortBuilderTest__ModelB::class);
314+
return $this
315+
->belongsTo(SortBuilderTest__ModelB::class)
316+
->where('a', '=', 'a');
297317
}
298318

299319
public function hasOneB(): HasOne {
300-
return $this->hasOne(SortBuilderTest__ModelB::class, 'model_a_id');
320+
return $this
321+
->hasOne(SortBuilderTest__ModelB::class, 'model_a_id')
322+
->where('b', '=', 'b');
301323
}
302324

303325
public function unsupported(): HasMany {
@@ -317,6 +339,14 @@ class SortBuilderTest__ModelB extends Model {
317339
*/
318340
public $table = 'table_b';
319341

342+
public function __construct() {
343+
self::unguard(true);
344+
parent::__construct([
345+
$this->getKeyName() => 56,
346+
'belongs_to_c_id' => 78,
347+
]);
348+
}
349+
320350
public function belongsToC(): BelongsTo {
321351
return $this->belongsTo(SortBuilderTest__ModelC::class);
322352
}
@@ -337,4 +367,11 @@ class SortBuilderTest__ModelC extends Model {
337367
* @var string
338368
*/
339369
public $table = 'table_c';
370+
371+
public function __construct() {
372+
self::unguard(true);
373+
parent::__construct([
374+
$this->getKeyName() => 90,
375+
]);
376+
}
340377
}

0 commit comments

Comments
 (0)