Skip to content

Fixed #13552: Overwriting columns with the same alias#13663

Merged
niden merged 2 commits intophalcon:4.0.xfrom
CameronHall:bugfix/issue-13552
Dec 22, 2018
Merged

Fixed #13552: Overwriting columns with the same alias#13663
niden merged 2 commits intophalcon:4.0.xfrom
CameronHall:bugfix/issue-13552

Conversation

@CameronHall
Copy link
Copy Markdown
Contributor

Hello!

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the Contributing Guidelines?
  • I have checked that another pull request for this purpose does not exist.
  • I wrote some tests for this PR.

Small description of change: Fixed Phalcon\Mvc\Model\Query::_prepareSelect overwriting columns with the same alias. This specific issue only appeared when you were selecting columns across
different tables with the same column name. The generated alias didn't factor in the different tables and any columns with the same name would get overridden.

Take this example from Phalcon\Test\Integration\Paginator\Adapter::testIssue13552. This snippet would generate the SQL below when the paginator tries to find out the total items.

$builder = $modelsManager->createBuilder()
    ->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('Robots.id > 2');

$paginate = (new QueryBuilder([
    "builder" => $builder,
    "limit"   => 1,
    "page"    => 2
]))->paginate();

Before:

SELECT DISTINCT 
    `RobotsParts_2`.`id` AS `id`,
     `RobotsParts_2`.`parts_id` AS `parts_id`
FROM `robots` AS `Robots`
LEFT JOIN `robots_parts` AS `RobotsParts` 
    ON `RobotsParts`.`robots_id` = `Robots`.`id`
LEFT JOIN `robots_parts` AS `RobotsParts_2` 
    ON `RobotsParts_2`.`robots_id` = `Robots`.`id`
GROUP BY
   `Robots`.`id`,
   `RobotsParts`.`id`,
   `RobotsParts`.`parts_id`,
   `RobotsParts_2`.`id`,
   `RobotsParts_2`.`parts_id`
HAVING `Robots`.`id` > 2

After:

SELECT DISTINCT 
    `Robots`.`id` AS `Robots_id`,
    `RobotsParts`.`id` AS `RobotsParts_id`,
    `RobotsParts`.`parts_id` AS `RobotsParts_parts_id`,
    `RobotsParts_2`.`id` AS `RobotsParts_2_id`,
    `RobotsParts_2`.`parts_id` AS `RobotsParts_2_parts_id`
FROM `robots` AS `Robots`
LEFT JOIN `robots_parts` AS `RobotsParts` 
    ON `RobotsParts`.`robots_id` = `Robots`.`id`
LEFT JOIN `robots_parts` AS `RobotsParts_2` 
    ON `RobotsParts_2`.`robots_id` = `Robots`.`id`
GROUP BY
   `Robots`.`id`,
   `RobotsParts`.`id`,
   `RobotsParts`.`parts_id`,
   `RobotsParts_2`.`id`,
   `RobotsParts_2`.`parts_id`

Thanks

Comment thread tests/integration/Paginator/Adapter/QueryBuilderCest.php
@niden
Copy link
Copy Markdown
Member

niden commented Dec 17, 2018

@CameronHall Something wrong with the tests bud.

@niden niden merged commit cb1f51f into phalcon:4.0.x Dec 22, 2018
@niden niden added the documentation Documentation required label Apr 9, 2019
@niden niden added 4.0 and removed documentation Documentation required labels Oct 15, 2019
@CameronHall CameronHall deleted the bugfix/issue-13552 branch October 24, 2019 11:36
@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
sergeyklay added a commit that referenced this pull request May 7, 2020
niden pushed a commit that referenced this pull request May 16, 2020
niden pushed a commit that referenced this pull request May 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A bug report status: medium Medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants