From e851f48e062d8a1bd8d94604da2da9c583f6c738 Mon Sep 17 00:00:00 2001 From: Cameron Hall Date: Fri, 14 Dec 2018 02:50:11 +1100 Subject: [PATCH] Fixed #13552: Overwriting columns with the same alias --- .gitignore | 1 - CHANGELOG-4.0.md | 1 + phalcon/mvc/model/query.zep | 3 +++ phalcon/paginator/adapter/querybuilder.zep | 5 ++--- .../Paginator/Adapter/QueryBuilderCest.php | 17 +++++++---------- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index ce9174f981c..20fcbbd4407 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,3 @@ docker-compose.yml build/gccarch tests/_cache .zephir/ -.temp/ diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index 1116b0f3303..085c074118a 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -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 diff --git a/phalcon/mvc/model/query.zep b/phalcon/mvc/model/query.zep index 0b2059f09c3..e22536bbf59 100644 --- a/phalcon/mvc/model/query.zep +++ b/phalcon/mvc/model/query.zep @@ -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" { diff --git a/phalcon/paginator/adapter/querybuilder.zep b/phalcon/paginator/adapter/querybuilder.zep index 32a60627b04..5ca36ac8b02 100644 --- a/phalcon/paginator/adapter/querybuilder.zep +++ b/phalcon/paginator/adapter/querybuilder.zep @@ -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 { diff --git a/tests/integration/Paginator/Adapter/QueryBuilderCest.php b/tests/integration/Paginator/Adapter/QueryBuilderCest.php index 56164b22bca..376a9ef2764 100644 --- a/tests/integration/Paginator/Adapter/QueryBuilderCest.php +++ b/tests/integration/Paginator/Adapter/QueryBuilderCest.php @@ -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); } }