DDC-2890 Fixed pagination with association order by.
This commit is contained in:
parent
841bdd5ca5
commit
be1cc14a9c
@ -3404,7 +3404,8 @@ class Parser
|
||||
* "SUBSTRING" "(" StringPrimary "," SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |
|
||||
* "TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")" |
|
||||
* "LOWER" "(" StringPrimary ")" |
|
||||
* "UPPER" "(" StringPrimary ")"
|
||||
* "UPPER" "(" StringPrimary ")" |
|
||||
* "IDENTITY" "(" SingleValuedAssociationPathExpression {"," string} ")"
|
||||
*
|
||||
* @return \Doctrine\ORM\Query\AST\Functions\FunctionNode
|
||||
*/
|
||||
|
@ -20,9 +20,10 @@ namespace Doctrine\ORM\Tools\Pagination;
|
||||
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\ORM\Query\TreeWalkerAdapter;
|
||||
use Doctrine\ORM\Query\AST\SelectStatement;
|
||||
use Doctrine\ORM\Query\AST\SelectExpression;
|
||||
use Doctrine\ORM\Query\AST\Functions\IdentityFunction;
|
||||
use Doctrine\ORM\Query\AST\PathExpression;
|
||||
use Doctrine\ORM\Query\AST\SelectExpression;
|
||||
use Doctrine\ORM\Query\AST\SelectStatement;
|
||||
|
||||
/**
|
||||
* Replaces the selectClause of the AST with a SELECT DISTINCT root.id equivalent.
|
||||
@ -99,21 +100,37 @@ class LimitSubqueryWalker extends TreeWalkerAdapter
|
||||
|
||||
if (isset($AST->orderByClause)) {
|
||||
foreach ($AST->orderByClause->orderByItems as $item) {
|
||||
if ($item->expression instanceof PathExpression) {
|
||||
$pathExpression = new PathExpression(
|
||||
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION,
|
||||
$item->expression->identificationVariable,
|
||||
$item->expression->field
|
||||
);
|
||||
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;
|
||||
$AST->selectClause->selectExpressions[] = new SelectExpression(
|
||||
$pathExpression,
|
||||
'_dctrn_ord' . $this->_aliasCounter++
|
||||
);
|
||||
if ( ! $item->expression instanceof PathExpression) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$AST->selectClause->selectExpressions[] = new SelectExpression(
|
||||
$this->createSelectExpressionItem($item->expression),
|
||||
'_dctrn_ord' . $this->_aliasCounter++
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$AST->selectClause->isDistinct = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve either an IdentityFunction (IDENTITY(u.assoc)) or a state field (u.name).
|
||||
*
|
||||
* @param \Doctrine\ORM\Query\AST\PathExpression $pathExpression
|
||||
*
|
||||
* @return \Doctrine\ORM\Query\AST\Functions\IdentityFunction
|
||||
*/
|
||||
private function createSelectExpressionItem(PathExpression $pathExpression)
|
||||
{
|
||||
if ($pathExpression->type === PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION) {
|
||||
$identity = new IdentityFunction('identity');
|
||||
|
||||
$identity->pathExpression = clone $pathExpression;
|
||||
|
||||
return $identity;
|
||||
}
|
||||
|
||||
return clone $pathExpression;
|
||||
}
|
||||
}
|
||||
|
@ -11,37 +11,60 @@ class LimitSubqueryWalkerTest extends PaginationTestCase
|
||||
{
|
||||
public function testLimitSubquery()
|
||||
{
|
||||
$query = $this->entityManager->createQuery(
|
||||
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a');
|
||||
$dql = 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
$limitQuery = clone $query;
|
||||
|
||||
$limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker'));
|
||||
|
||||
$this->assertEquals(
|
||||
"SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id", $limitQuery->getSql()
|
||||
"SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
}
|
||||
|
||||
public function testLimitSubqueryWithSort()
|
||||
{
|
||||
$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');
|
||||
$dql = 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
$limitQuery = clone $query;
|
||||
|
||||
$limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker'));
|
||||
|
||||
$this->assertEquals(
|
||||
"SELECT DISTINCT m0_.id AS id_0, m0_.title AS title_1 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id ORDER BY m0_.title ASC", $limitQuery->getSql()
|
||||
"SELECT DISTINCT m0_.id AS id_0, m0_.title AS title_1 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id ORDER BY m0_.title ASC",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
}
|
||||
|
||||
public function testCountQuery_MixedResultsWithName()
|
||||
{
|
||||
$query = $this->entityManager->createQuery(
|
||||
'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a');
|
||||
$dql = 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
$limitQuery = clone $query;
|
||||
|
||||
$limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker'));
|
||||
|
||||
$this->assertEquals(
|
||||
"SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1 FROM Author a0_", $limitQuery->getSql()
|
||||
"SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1 FROM Author a0_",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group DDC-2890
|
||||
*/
|
||||
public function testLimitSubqueryWithSortOnAssociation()
|
||||
{
|
||||
$dql = 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p ORDER BY p.author';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
$limitQuery = clone $query;
|
||||
|
||||
$limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker'));
|
||||
|
||||
$this->assertEquals(
|
||||
"SELECT DISTINCT m0_.id AS id_0, m0_.author_id AS sclr_1 FROM MyBlogPost m0_ ORDER BY m0_.author_id ASC",
|
||||
$limitQuery->getSql()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user