Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC-3336 - undefined property with paginator walker and scalar expression in ORDER BY clause #1210

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Doctrine\ORM\Tools\Pagination;

use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
Expand Down Expand Up @@ -200,12 +201,14 @@ public function preserveSqlOrdering(SelectStatement $AST, array $sqlIdentifier,
$orderBy = array();
if (isset($AST->orderByClause)) {
foreach ($AST->orderByClause->orderByItems as $item) {
$possibleAliases = (is_object($item->expression))
? array_keys($this->rsm->fieldMappings, $item->expression->field)
: array_keys($this->rsm->scalarMappings, $item->expression);
$expression = $item->expression;

$possibleAliases = $expression instanceof PathExpression
? array_keys($this->rsm->fieldMappings, $expression->field)
: array_keys($this->rsm->scalarMappings, $expression);

foreach ($possibleAliases as $alias) {
if (!is_object($item->expression) || $this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) {
if (!is_object($expression) || $this->rsm->columnOwnerMap[$alias] == $expression->identificationVariable) {
$sqlOrderColumns[] = $alias;
$orderBy[] = $alias . ' ' . $item->type;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Doctrine\Tests\ORM\Tools\Pagination;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\ORM\Query;

class LimitSubqueryOutputWalkerTest extends PaginationTestCase
Expand All @@ -22,7 +25,7 @@ public function testLimitSubquery()
public function testLimitSubqueryWithSortPg()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);

$query = $this->entityManager->createQuery(
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title');
Expand All @@ -39,7 +42,7 @@ public function testLimitSubqueryWithSortPg()
public function testLimitSubqueryWithScalarSortPg()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);

$query = $this->entityManager->createQuery(
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity'
Expand All @@ -58,7 +61,7 @@ public function testLimitSubqueryWithScalarSortPg()
public function testLimitSubqueryWithMixedSortPg()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);

$query = $this->entityManager->createQuery(
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
Expand All @@ -77,7 +80,7 @@ public function testLimitSubqueryWithMixedSortPg()
public function testLimitSubqueryWithHiddenScalarSortPg()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);

$query = $this->entityManager->createQuery(
'SELECT u, g, COUNT(g.id) AS hidden g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
Expand All @@ -96,7 +99,7 @@ public function testLimitSubqueryWithHiddenScalarSortPg()
public function testLimitSubqueryPg()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);

$this->testLimitSubquery();

Expand All @@ -106,7 +109,7 @@ public function testLimitSubqueryPg()
public function testLimitSubqueryWithSortOracle()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);

$query = $this->entityManager->createQuery(
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title');
Expand All @@ -124,7 +127,7 @@ public function testLimitSubqueryWithSortOracle()
public function testLimitSubqueryWithScalarSortOracle()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);

$query = $this->entityManager->createQuery(
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity'
Expand All @@ -144,7 +147,7 @@ public function testLimitSubqueryWithScalarSortOracle()
public function testLimitSubqueryWithMixedSortOracle()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);

$query = $this->entityManager->createQuery(
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
Expand All @@ -164,7 +167,7 @@ public function testLimitSubqueryWithMixedSortOracle()
public function testLimitSubqueryOracle()
{
$odp = $this->entityManager->getConnection()->getDatabasePlatform();
$this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform);
$this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);

$query = $this->entityManager->createQuery(
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a');
Expand All @@ -179,7 +182,7 @@ public function testLimitSubqueryOracle()
$this->entityManager->getConnection()->setDatabasePlatform($odp);
}

public function testCountQuery_MixedResultsWithName()
public function testCountQueryMixedResultsWithName()
{
$query = $this->entityManager->createQuery(
'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a');
Expand All @@ -190,5 +193,23 @@ public function testCountQuery_MixedResultsWithName()
"SELECT DISTINCT id_0 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1, sum(a0_.name) AS sclr_2 FROM Author a0_) dctrn_result", $limitQuery->getSql()
);
}

/**
* @group DDC-3336
*/
public function testCountQueryWithArithmeticOrderByCondition()
{
$query = $this->entityManager->createQuery(
'SELECT a FROM Doctrine\\Tests\\ORM\\Tools\\Pagination\\Author a ORDER BY (1 - 1000) * 1 DESC'
);
$this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());

$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');

$this->assertSame(
'SELECT DISTINCT id_0 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1 FROM Author a0_ ORDER BY (1 - 1000) * 1 DESC) dctrn_result',
$query->getSQL()
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

abstract class PaginationTestCase extends OrmTestCase
{
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
public $entityManager;

public function setUp()
Expand Down