Skip to content

Commit 9ea862e

Browse files
committed
[graphql] @sortBy: MorphOne sorting fix.
1 parent d9b631d commit 9ea862e

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/SortBy/SortBuilder.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Relations\MorphOne;
99
use Illuminate\Database\Eloquent\Relations\Relation;
1010
use Illuminate\Database\Query\Builder as QueryBuilder;
11+
use Illuminate\Database\Query\JoinClause;
1112
use LastDragon_ru\LaraASP\GraphQL\Helpers\ModelHelper;
1213
use LastDragon_ru\LaraASP\GraphQL\PackageTranslator;
1314
use LogicException;
@@ -157,7 +158,7 @@ protected function processRelation(
157158
? "{$parentAlias}.{$relation->getForeignKeyName()}"
158159
: $relation->getQualifiedForeignKeyName(),
159160
);
160-
} elseif ($relation instanceof HasOne || $relation instanceof MorphOne) {
161+
} elseif ($relation instanceof HasOne) {
161162
$builder = $builder->leftJoinSub(
162163
$relation->getQuery(),
163164
$alias,
@@ -167,6 +168,25 @@ protected function processRelation(
167168
? "{$parentAlias}.{$relation->getLocalKeyName()}"
168169
: $relation->getQualifiedParentKeyName(),
169170
);
171+
} elseif ($relation instanceof MorphOne) {
172+
$builder = $builder->leftJoinSub(
173+
$relation->getQuery(),
174+
$alias,
175+
static function (JoinClause $join) use ($relation, $alias, $parentAlias): void {
176+
$join->on(
177+
"{$alias}.{$relation->getForeignKeyName()}",
178+
'=',
179+
$parentAlias
180+
? "{$parentAlias}.{$relation->getLocalKeyName()}"
181+
: $relation->getQualifiedParentKeyName(),
182+
);
183+
$join->where(
184+
"{$alias}.{$relation->getMorphType()}",
185+
'=',
186+
$relation->getMorphClass(),
187+
);
188+
},
189+
);
170190
} else {
171191
throw new LogicException('O_o => Please contact to developer.');
172192
}

src/SortBy/SortBuilderTest.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ static function (): EloquentBuilder {
190190
' "table_alias_1"."name" as "table_alias_1_name",'.
191191
' "table_alias_1"."created_at" as "table_alias_1_created_at" '.
192192
'from "table_a" '.
193-
'left join (select * from "table_b" where "table_b"."id" = ? and "a" = ?)'.
193+
'left join (select * from "table_b" where "a" = ?)'.
194194
' as "table_alias_0" on "table_alias_0"."id" = "table_a"."belongs_to_b_id" '.
195-
'left join (select * from "table_c" where "table_c"."id" = ?)'.
195+
'left join (select * from "table_c")'.
196196
' as "table_alias_1" on "table_alias_1"."id" = "table_alias_0"."belongs_to_c_id" '.
197197
'order by'.
198198
' "table_alias_0_name" asc,'.
@@ -201,9 +201,7 @@ static function (): EloquentBuilder {
201201
' "table_alias_1_created_at" desc,'.
202202
' "table_a"."name" asc',
203203
'bindings' => [
204-
34,
205204
'a',
206-
78,
207205
],
208206
],
209207
static function (): EloquentBuilder {
@@ -242,10 +240,9 @@ static function (): EloquentBuilder {
242240
' "table_alias_1"."created_at" as "table_alias_1_created_at" '.
243241
'from "table_a" '.
244242
'left join (select * from "table_b" where'.
245-
' "table_b"."model_a_id" = ? and "table_b"."model_a_id" is not null and "b" = ?'.
243+
' "b" = ?'.
246244
') as "table_alias_0" on "table_alias_0"."model_a_id" = "table_a"."id" '.
247-
'left join (select * from "table_c" where'.
248-
' "table_c"."model_b_id" = ? and "table_c"."model_b_id" is not null'.
245+
'left join (select * from "table_c"'.
249246
') as "table_alias_1" on "table_alias_1"."model_b_id" = "table_alias_0"."id" '.
250247
'order by'.
251248
' "table_alias_0_name" asc,'.
@@ -254,9 +251,7 @@ static function (): EloquentBuilder {
254251
' "table_alias_1_created_at" desc,'.
255252
' "table_a"."name" asc',
256253
'bindings' => [
257-
12,
258254
'b',
259-
56,
260255
],
261256
],
262257
static function (): EloquentBuilder {
@@ -295,24 +290,21 @@ static function (): EloquentBuilder {
295290
' "table_alias_1"."created_at" as "table_alias_1_created_at" '.
296291
'from "table_a" '.
297292
'left join (select * from "table_b" where'.
298-
' "table_b"."morphable_a_id" = ? and "table_b"."morphable_a_id" is not null and'.
299-
' "table_b"."morphable_a_type" = ? and "c" = ?'.
300-
') as "table_alias_0" on "table_alias_0"."morphable_a_id" = "table_a"."id" '.
301-
'left join (select * from "table_c" where'.
302-
' "table_c"."morphable_b_id" = ? and "table_c"."morphable_b_id" is not null and'.
303-
' "table_c"."morphable_b_type" = ?'.
304-
') as "table_alias_1" on "table_alias_1"."morphable_b_id" = "table_alias_0"."id" '.
293+
' "c" = ?'.
294+
') as "table_alias_0" on "table_alias_0"."morphable_a_id" = "table_a"."id" and'.
295+
' "table_alias_0"."morphable_a_type" = ? '.
296+
'left join (select * from "table_c"'.
297+
') as "table_alias_1" on "table_alias_1"."morphable_b_id" = "table_alias_0"."id" and'.
298+
' "table_alias_1"."morphable_b_type" = ? '.
305299
'order by'.
306300
' "table_alias_0_name" asc,'.
307301
' "table_alias_0_created_at" desc,'.
308302
' "table_alias_1_name" desc,'.
309303
' "table_alias_1_created_at" desc,'.
310304
' "table_a"."name" asc',
311305
'bindings' => [
312-
12,
313-
SortBuilderTest__ModelA::class,
314306
'c',
315-
56,
307+
SortBuilderTest__ModelA::class,
316308
SortBuilderTest__ModelB::class,
317309
],
318310
],

0 commit comments

Comments
 (0)