Skip to content

Commit c61b444

Browse files
author
Marcel Gwerder
committed
Fixed an issue where columns aliases were turned into qualified column names
1 parent 02fd69b commit c61b444

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Parser.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,13 @@ protected function isRelation($model, $relationName)
726726
*/
727727
protected function getQualifiedColumnName($column, $table = null)
728728
{
729-
if (strpos($column, '.') === false) {
729+
//Check whether there is a matching column expression that contains an
730+
//alias and should therefore not be turned into a qualified column name.
731+
$isAlias = !empty(array_filter($this->query->columns ?: [], function($column) {
732+
return stripos($column, ' as ') !== false;
733+
}));
734+
735+
if (strpos($column, '.') === false && !$isAlias) {
730736
return $table ?: $this->query->from.'.'.$column;
731737
} else {
732738
return $column;

tests/ApiHandlerTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public function testGetBuilder()
242242
$this->assertContains(['column' => 'comments.created_at', 'direction' => 'asc'], $orders);
243243

244244
//
245-
// Fulltext search
245+
// Default fulltext search
246246
//
247247

248248
$builder = $this->apiHandler->parseMultiple($post, ['title', 'description'], ['_q' => 'Something to search'])->getBuilder();
@@ -265,9 +265,17 @@ public function testGetBuilder()
265265
}
266266
}
267267

268-
$builder = $this->apiHandler->parseMultiple($post, ['title', 'description'], ['_q' => 'Something to search'])->getBuilder();
268+
//
269+
// Native fulltext search
270+
//
271+
272+
$builder = $this->apiHandler->parseMultiple($post, ['title', 'description'], ['_q' => 'Something to search', '_sort' => '_score'])->getBuilder();
269273
$queryBuilder = $builder->getQuery();
270274

275+
//Test alias column in sort
276+
$orders = $queryBuilder->orders;
277+
$this->assertContains(['column' => '_score', 'direction' => 'asc'], $orders);
278+
271279
//Test the where
272280
$wheres = $queryBuilder->wheres;
273281
$this->assertEquals(['type' => 'raw', 'sql' => 'MATCH(posts.title,posts.description) AGAINST("Something to search" IN BOOLEAN MODE)', 'boolean' => 'and'], $wheres[0]);

0 commit comments

Comments
 (0)