Skip to content

Commit 9af3cd6

Browse files
committed
[graphql] SearchBy: Fixed column prefix for Relation condition (not all whereHas adds prefix)
1 parent cdababa commit 9af3cd6

File tree

2 files changed

+75
-33
lines changed

2 files changed

+75
-33
lines changed

src/SearchBy/Operators/Complex/Relation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,15 @@ public function apply(
8888
static function (
8989
EloquentBuilder|QueryBuilder $builder,
9090
) use (
91+
$relation,
9192
$search,
9293
$alias,
9394
$has,
9495
): EloquentBuilder|QueryBuilder {
96+
if ($alias === $relation->getRelationCountHash(false)) {
97+
$alias = null;
98+
}
99+
95100
return is_array($has)
96101
? $search->process($builder, $has, $alias)
97102
: $builder;

src/SearchBy/Operators/Complex/RelationTest.php

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testApply(
6363
*/
6464
public function dataProviderApply(): array {
6565
return [
66-
'query builder not supported' => [
66+
'query builder not supported' => [
6767
new SearchLogicException(sprintf(
6868
'Operator `%s` can not be used with `%s`.',
6969
(new Relation())->getName(),
@@ -75,61 +75,60 @@ static function (self $test): QueryBuilder {
7575
'test',
7676
[],
7777
],
78-
'not a relation' => [
78+
'not a relation' => [
7979
new LogicException(sprintf(
8080
'Property `%s` is not a relation.',
8181
'delete',
8282
)),
8383
static function (): EloquentBuilder {
84-
return RelationTest__Model::query();
84+
return RelationTest__ModelA::query();
8585
},
8686
'delete',
8787
[],
8888
],
89-
'{has: yes}' => [
89+
'{has: yes}' => [
9090
[
91-
'sql' => 'select * from "tmp" where exists ('.
92-
'select * from "tmp" as "table_alias_0" '.
93-
'where "tmp"."id" = "table_alias_0"."relation_test___model_id"'.
91+
'sql' => 'select * from "table_a" where exists ('.
92+
'select * from "table_b" '.
93+
'where "table_a"."id" = "table_b"."table_a_id"'.
9494
')',
9595
'bindings' => [],
9696
],
9797
static function (): EloquentBuilder {
98-
return RelationTest__Model::query();
98+
return RelationTest__ModelA::query();
9999
},
100100
'test',
101101
[
102102
'has' => 'yes',
103103
],
104104
],
105-
'{has: yes, not: yes}' => [
105+
'{has: yes, not: yes}' => [
106106
[
107-
'sql' => 'select * from "tmp" where not exists ('.
108-
'select * from "tmp" as "table_alias_0" '.
109-
'where "tmp"."id" = "table_alias_0"."relation_test___model_id"'.
107+
'sql' => 'select * from "table_a" where not exists ('.
108+
'select * from "table_b" '.
109+
'where "table_a"."id" = "table_b"."table_a_id"'.
110110
')',
111111
'bindings' => [],
112112
],
113113
static function (): EloquentBuilder {
114-
return RelationTest__Model::query();
114+
return RelationTest__ModelA::query();
115115
},
116116
'test',
117117
[
118118
'has' => 'yes',
119119
'not' => 'yes',
120120
],
121121
],
122-
'{has: {property: {eq: 1}}}' => [
122+
'{has: {property: {eq: 1}}}' => [
123123
[
124-
'sql' => 'select * from "tmp" where exists ('.
125-
'select * from "tmp" as "table_alias_0" where '.
126-
'"tmp"."id" = "table_alias_0"."relation_test___model_id" '.
127-
'and "table_alias_0"."property" = ?'.
124+
'sql' => 'select * from "table_a" where exists ('.
125+
'select * from "table_b" where '.
126+
'"table_a"."id" = "table_b"."table_a_id" and "property" = ?'.
128127
')',
129128
'bindings' => [123],
130129
],
131130
static function (): EloquentBuilder {
132-
return RelationTest__Model::query();
131+
return RelationTest__ModelA::query();
133132
},
134133
'test',
135134
[
@@ -140,33 +139,33 @@ static function (): EloquentBuilder {
140139
],
141140
],
142141
],
143-
'{has: yes, eq: 1}' => [
142+
'{has: yes, eq: 1}' => [
144143
[
145-
'sql' => 'select * from "tmp" where ('.
146-
'select count(*) from "tmp" as "table_alias_0" where '.
147-
'"tmp"."id" = "table_alias_0"."relation_test___model_id"'.
144+
'sql' => 'select * from "table_a" where ('.
145+
'select count(*) from "table_b" where '.
146+
'"table_a"."id" = "table_b"."table_a_id"'.
148147
') = 345',
149148
'bindings' => [/* strange */],
150149
],
151150
static function (): EloquentBuilder {
152-
return RelationTest__Model::query();
151+
return RelationTest__ModelA::query();
153152
},
154153
'test',
155154
[
156155
'has' => 'yes',
157156
'eq' => 345,
158157
],
159158
],
160-
'{has: yes, eq: 1, not: yes}' => [
159+
'{has: yes, eq: 1, not: yes}' => [
161160
[
162-
'sql' => 'select * from "tmp" where ('.
163-
'select count(*) from "tmp" as "table_alias_0" '.
164-
'where "tmp"."id" = "table_alias_0"."relation_test___model_id"'.
161+
'sql' => 'select * from "table_a" where ('.
162+
'select count(*) from "table_b" '.
163+
'where "table_a"."id" = "table_b"."table_a_id"'.
165164
') != 345',
166165
'bindings' => [/* strange */],
167166
],
168167
static function (): EloquentBuilder {
169-
return RelationTest__Model::query();
168+
return RelationTest__ModelA::query();
170169
},
171170
'test',
172171
[
@@ -175,12 +174,12 @@ static function (): EloquentBuilder {
175174
'eq' => 345,
176175
],
177176
],
178-
'{has: yes, eq: 1, gt: 2}' => [
177+
'{has: yes, eq: 1, gt: 2}' => [
179178
new SearchLogicException(
180179
'Only one comparison operator allowed, found: `eq`, `gt`',
181180
),
182181
static function (): EloquentBuilder {
183-
return RelationTest__Model::query();
182+
return RelationTest__ModelA::query();
184183
},
185184
'test',
186185
[
@@ -189,6 +188,27 @@ static function (): EloquentBuilder {
189188
'gt' => 2,
190189
],
191190
],
191+
'{has: {property: {eq: 1}}} (own)' => [
192+
[
193+
'sql' => 'select * from "table_a" where exists ('.
194+
'select * from "table_a" as "table_alias_0" where '.
195+
'"table_a"."id" = "table_alias_0"."relation_test___model_a_id" '.
196+
'and "table_alias_0"."property" = ?'.
197+
')',
198+
'bindings' => [123],
199+
],
200+
static function (): EloquentBuilder {
201+
return RelationTest__ModelA::query();
202+
},
203+
'a',
204+
[
205+
'has' => [
206+
'property' => [
207+
'eq' => 123,
208+
],
209+
],
210+
],
211+
],
192212
];
193213
}
194214
// </editor-fold>
@@ -201,15 +221,32 @@ static function (): EloquentBuilder {
201221
* @internal
202222
* @noinspection PhpMultipleClassesDeclarationsInOneFile
203223
*/
204-
class RelationTest__Model extends Model {
224+
class RelationTest__ModelA extends Model {
205225
/**
206226
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
207227
*
208228
* @var string
209229
*/
210-
public $table = 'tmp';
230+
public $table = 'table_a';
211231

212232
public function test(): HasOne {
233+
return $this->hasOne(RelationTest__ModelB::class, 'table_a_id');
234+
}
235+
236+
public function a(): HasOne {
213237
return $this->hasOne(static::class);
214238
}
215239
}
240+
241+
/**
242+
* @internal
243+
* @noinspection PhpMultipleClassesDeclarationsInOneFile
244+
*/
245+
class RelationTest__ModelB extends Model {
246+
/**
247+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
248+
*
249+
* @var string
250+
*/
251+
public $table = 'table_b';
252+
}

0 commit comments

Comments
 (0)