Skip to content

Commit

Permalink
Fixed phalcon#13552: Overwriting columns with the same alias
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronHall committed Dec 17, 2018
1 parent 4f9a8b2 commit fbabafd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@ docker-compose.yml
build/gccarch
tests/_cache
.zephir/
.temp/
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions phalcon/mvc/model/query.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
5 changes: 2 additions & 3 deletions phalcon/paginator/adapter/querybuilder.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions tests/integration/Paginator/Adapter/QueryBuilderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(5, $paginate->last);
$I->assertEquals(43, $paginate->total_items);
}
}

0 comments on commit fbabafd

Please sign in to comment.