diff --git a/framework/db/QueryBuilder.php b/framework/db/QueryBuilder.php index 0709634529d..3aeb57fee3c 100644 --- a/framework/db/QueryBuilder.php +++ b/framework/db/QueryBuilder.php @@ -751,7 +751,21 @@ public function buildWhere($condition, &$params) */ public function buildGroupBy($columns) { - return empty($columns) ? '' : 'GROUP BY ' . $this->buildColumns($columns); + if (empty($columns)) { + return ''; + } + foreach ($columns as $i => $column) { + if ($column instanceof Expression) { + $columns[$i] = $column->expression; + // TODO +// foreach ($direction->params as $n => $v) { +// $params[$n] = $v; +// } + } elseif (strpos($column, '(') === false) { + $columns[$i] = $this->db->quoteColumnName($column); + } + } + return 'GROUP BY ' . implode(', ', $columns); } /** @@ -800,6 +814,10 @@ public function buildOrderBy($columns) foreach ($columns as $name => $direction) { if ($direction instanceof Expression) { $orders[] = $direction->expression; + // TODO +// foreach ($direction->params as $n => $v) { +// $params[$n] = $v; +// } } else { $orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : ''); } diff --git a/tests/framework/db/QueryBuilderTest.php b/tests/framework/db/QueryBuilderTest.php index 4478c44091b..4ae1943c6e4 100644 --- a/tests/framework/db/QueryBuilderTest.php +++ b/tests/framework/db/QueryBuilderTest.php @@ -601,7 +601,7 @@ public function testOrderBy() ->from('operations') ->orderBy('name ASC, date DESC'); list ($sql, $params) = $this->getQueryBuilder()->build($query); - $expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY `name`, `date` DESC'); + $expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC'); $this->assertEquals($expected, $sql); $this->assertEmpty($params); @@ -611,7 +611,7 @@ public function testOrderBy() ->from('operations') ->orderBy(['name' => SORT_ASC, 'date' => SORT_DESC]); list ($sql, $params) = $this->getQueryBuilder()->build($query); - $expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY `name`, `date` DESC'); + $expected = $this->replaceQuotes('SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC'); $this->assertEquals($expected, $sql); $this->assertEmpty($params); @@ -645,7 +645,7 @@ public function testGroupBy() ->from('operations') ->groupBy('name, date'); list ($sql, $params) = $this->getQueryBuilder()->build($query); - $expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY `name`, `date`'); + $expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY [[name]], [[date]]'); $this->assertEquals($expected, $sql); $this->assertEmpty($params); @@ -655,7 +655,7 @@ public function testGroupBy() ->from('operations') ->groupBy(['name', 'date']); list ($sql, $params) = $this->getQueryBuilder()->build($query); - $expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY `name`, `date`'); + $expected = $this->replaceQuotes('SELECT * FROM [[operations]] GROUP BY [[name]], [[date]]'); $this->assertEquals($expected, $sql); $this->assertEmpty($params);