Permalink
Browse files

Fixed #13552: Overwriting columns with the same alias

  • Loading branch information...
CameronHall committed Dec 13, 2018
1 parent 4f9a8b2 commit e851f48e062d8a1bd8d94604da2da9c583f6c738
@@ -55,4 +55,3 @@ docker-compose.yml
build/gccarch
tests/_cache
.zephir/
.temp/
@@ -59,6 +59,7 @@
- Scope SQL Column Aliases (on nesting level), in `Phalcon\Mvc\Model\Query`, to prevent overwrite _root_ query's `_sqlColumnAliases` by sub-queries. [#13006](https://github.com/phalcon/cphalcon/issues/13006), [#12548](https://github.com/phalcon/cphalcon/issues/12548) and [#1731](https://github.com/phalcon/cphalcon/issues/1731)
- CLI parameters now work like MVC parameters [#12375](https://github.com/phalcon/cphalcon/pull/12375)
- Changed `Phalcon\Db\Dialect\Postgresql::addPrimaryKey` to make primary key contraints names unique by prefixing them with the table name. [#12629](https://github.com/phalcon/cphalcon/pull/12629)
- Fixed `Phalcon\Mvc\Model\Query::_prepareSelect` overwriting columns with the same alias [#13552](https://github.com/phalcon/cphalcon/issues/13552)

## Removed
- PHP < 7.2 no longer supported
@@ -2034,6 +2034,9 @@ class Query implements QueryInterface, InjectionAwareInterface
* "balias" is the best alias chosen for the column
*/
if fetch alias, sqlColumn["balias"] {
if isset column["column"]["domain"] {
let alias = column["column"]["domain"] . "_" . alias;
}
let sqlColumns[alias] = sqlColumn;
} else {
if sqlColumn["type"] == "scalar" {
@@ -116,7 +116,7 @@ class QueryBuilder extends Adapter
var originalBuilder, builder, totalBuilder, totalPages,
limit, numberPage, number, query, previous, items, totalQuery,
result, row, rowcount, next, sql, columns, db, hasHaving, hasGroup,
model, modelClass, dbService;
model, modelClass, dbService, groups, groupColumn;

let originalBuilder = this->_builder;
let columns = this->_columns;
@@ -164,7 +164,7 @@ class QueryBuilder extends Adapter

let hasHaving = !empty totalBuilder->getHaving();

var groups = totalBuilder->getGroupBy();
let groups = totalBuilder->getGroupBy();

let hasGroup = !empty groups;

@@ -185,7 +185,6 @@ class QueryBuilder extends Adapter
* Change 'COUNT()' parameters, when the query contains 'GROUP BY'
*/
if hasGroup {
var groupColumn;
if typeof groups == "array" {
let groupColumn = implode(", ", groups);
} else {
@@ -177,24 +177,21 @@ public function testIssue13552(IntegrationTester $I)
$modelsManager = $this->getService('modelsManager');
$builder = $modelsManager->createBuilder()
->columns("COUNT(*) as robos_count")
->columns("Robots.*")
->from(['Robots' => Robots::class])
->join(RobotsParts::class, "RobotsParts.robots_id = Robots.id", "RobotsParts", "LEFT")
->join(RobotsParts::class, "RobotsParts_2.robots_id = Robots.id", "RobotsParts_2", "LEFT")
->groupBy('Robots.id, RobotsParts.id, RobotsParts.parts_id, RobotsParts_2.id, RobotsParts_2.parts_id')
->having('MAX(Robots.year) > 1970');
// var_dump($builder->getQuery()->getSql());
->groupBy('Robots.id, RobotsParts.id, RobotsParts.parts_id')
->having('Robots.id > 2');
$paginate = (new QueryBuilder(
[
"builder" => $builder,
"limit" => 1,
"page" => 2
"limit" => 10,
"page" => 1
]
))->paginate();
$I->assertEquals(4, $paginate->last);
$I->assertEquals(4, $paginate->total_items);
$I->assertEquals(1, $paginate->last);
$I->assertEquals(1, $paginate->total_items);
}
}

0 comments on commit e851f48

Please sign in to comment.