Skip to content

Commit 31a2fd5

Browse files
author
Rurik Wind
committed
Fix using convert_relation_table_name_strategy of "none"
1 parent 3fc0ded commit 31a2fd5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Concerns/AddsFieldsToQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getRequestedFieldsForRelatedTable(string $relation, ?string $tab
7676
} elseif ($strategy === 'camelCase' && $tableName) {
7777
$possibleRelatedNames[] = Str::camel($tableName);
7878
} elseif ($strategy === 'none') {
79-
$possibleRelatedNames = $tableName;
79+
$possibleRelatedNames[] = $tableName;
8080
}
8181

8282
// Remove any null values

tests/FieldsTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,31 @@
261261
$this->assertQueryLogContains('select `related_models`.`name` from `related_models`');
262262
});
263263

264+
it('can fetch only requested string columns from an included model with relation convert strategy "none"', function () {
265+
config(['query-builder.convert_relation_names_to_snake_case_plural' => false]);
266+
config(['query-builder.convert_relation_table_name_strategy' => 'none']);
267+
RelatedModel::create([
268+
'test_model_id' => $this->model->id,
269+
'name' => 'related',
270+
]);
271+
272+
$request = new Request([
273+
'fields' => 'id,relatedModels.name',
274+
'include' => ['relatedModels'],
275+
]);
276+
277+
$queryBuilder = QueryBuilder::for(TestModel::class, $request)
278+
->allowedFields('relatedModels.name', 'id')
279+
->allowedIncludes('relatedModels');
280+
281+
DB::enableQueryLog();
282+
283+
$queryBuilder->first()->relatedModels;
284+
285+
$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
286+
$this->assertQueryLogContains('select `related_models`.`name` from `related_models`');
287+
});
288+
264289
it('can fetch only requested string columns from an included model jsonApi format with field conversion', function () {
265290
config(['query-builder.convert_field_names_to_snake_case' => true]);
266291
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);

0 commit comments

Comments
 (0)