Skip to content

Commit

Permalink
Query: Fix that aliases are ignored in withColumns()
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Apr 12, 2023
1 parent c5f2fda commit 3b7dca6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,11 @@ public function withColumns($columns)
$tableName = $this->getModel()->getTableAlias();

$qualifiedColumns = [];
foreach ((array) $columns as $column) {
foreach ((array) $columns as $alias => $column) {
if (! $column instanceof ExpressionInterface) {
$qualifiedColumns[] = $this->getResolver()->qualifyPath($column, $tableName);
$qualifiedColumns[$alias] = $this->getResolver()->qualifyPath($column, $tableName);
} else {
$qualifiedColumns[] = $column;
$qualifiedColumns[$alias] = $column;
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,23 @@ public function testWithColumnsSupportsExpressions()
);
}

public function testWithColumnsHandlesCustomAliasesCorrectly()
{
$query = (new Query())
->setModel(new User())
->columns('username');

$query->withColumns([
'my_pw' => 'password',
'one' => new Expression('1')
]);

$this->assertSql(
'SELECT user.password AS my_pw, (1) AS one, user.username FROM user',
$query->assembleSelect()
);
}

public function testWithoutColumnsPreventsSelection()
{
$query = (new Query())
Expand Down

0 comments on commit 3b7dca6

Please sign in to comment.