1
0
mirror of synced 2025-01-09 02:27:10 +03:00

Root selection according other pagination tools

changed root selection in Walkers from looping queryComponents to using
$AST->fromClause as other walkers have
This commit is contained in:
František Bereň 2014-07-30 15:55:14 +02:00
parent 114bd2435f
commit a37f99f242
3 changed files with 31 additions and 66 deletions

View File

@ -50,40 +50,24 @@ class CountWalker extends TreeWalkerAdapter
throw new \RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination');
}
$rootComponents = array();
foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) {
$isParent = array_key_exists('parent', $qComp)
&& $qComp['parent'] === null
&& $qComp['nestingLevel'] == 0
;
if ($isParent) {
foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) {
$isRoot = $identificationVariableDeclaration->rangeVariableDeclaration
&& $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias
&& $identificationVariableDeclaration->rangeVariableDeclaration->isRoot
;
if ($isRoot) {
$rootComponents[] = array($dqlAlias => $qComp);
break;
}
}
}
}
if (count($rootComponents) > 1) {
$queryComponents = $this->_getQueryComponents();
// Get the root entity and alias from the AST fromClause
$from = $AST->fromClause->identificationVariableDeclarations;
if (count($from) > 1) {
throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
}
$root = reset($rootComponents);
$parentName = key($root);
$parent = current($root);
$identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName();
$rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable;
$rootClass = $queryComponents[$rootAlias]['metadata'];
$identifierFieldName = $rootClass->getSingleIdentifierFieldName();
$pathType = PathExpression::TYPE_STATE_FIELD;
if (isset($parent['metadata']->associationMappings[$identifierFieldName])) {
if (isset($rootClass->associationMappings[$identifierFieldName])) {
$pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
}
$pathExpression = new PathExpression(
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName,
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias,
$identifierFieldName
);
$pathExpression->type = $pathType;

View File

@ -60,37 +60,34 @@ class LimitSubqueryWalker extends TreeWalkerAdapter
*/
public function walkSelectStatement(SelectStatement $AST)
{
$parent = null;
$parentName = null;
$queryComponents = $this->_getQueryComponents();
// Get the root entity and alias from the AST fromClause
$from = $AST->fromClause->identificationVariableDeclarations;
$rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable;
$rootClass = $queryComponents[$rootAlias]['metadata'];
$selectExpressions = array();
foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) {
foreach ($queryComponents as $dqlAlias => $qComp) {
// Preserve mixed data in query for ordering.
if (isset($qComp['resultVariable'])) {
$selectExpressions[] = new SelectExpression($qComp['resultVariable'], $dqlAlias);
continue;
}
if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) {
$parent = $qComp;
$parentName = $dqlAlias;
continue;
}
}
$identifier = $parent['metadata']->getSingleIdentifierFieldName();
if (isset($parent['metadata']->associationMappings[$identifier])) {
$identifier = $rootClass->getSingleIdentifierFieldName();
if (isset($rootClass->associationMappings[$identifier])) {
throw new \RuntimeException("Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator.");
}
$this->_getQuery()->setHint(
self::IDENTIFIER_TYPE,
Type::getType($parent['metadata']->getTypeOfField($identifier))
Type::getType($rootClass->getTypeOfField($identifier))
);
$pathExpression = new PathExpression(
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION,
$parentName,
$rootAlias,
$identifier
);
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;

View File

@ -71,39 +71,23 @@ class WhereInWalker extends TreeWalkerAdapter
*/
public function walkSelectStatement(SelectStatement $AST)
{
$rootComponents = array();
foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) {
$isParent = array_key_exists('parent', $qComp)
&& $qComp['parent'] === null
&& $qComp['nestingLevel'] == 0
;
if ($isParent) {
foreach($AST->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) {
$isRoot = $identificationVariableDeclaration->rangeVariableDeclaration
&& $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable == $dqlAlias
&& $identificationVariableDeclaration->rangeVariableDeclaration->isRoot
;
if ($isRoot) {
$rootComponents[] = array($dqlAlias => $qComp);
break;
}
}
}
}
if (count($rootComponents) > 1) {
$queryComponents = $this->_getQueryComponents();
// Get the root entity and alias from the AST fromClause
$from = $AST->fromClause->identificationVariableDeclarations;
if (count($from) > 1) {
throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
}
$root = reset($rootComponents);
$parentName = key($root);
$parent = current($root);
$identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName();
$rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable;
$rootClass = $queryComponents[$rootAlias]['metadata'];
$identifierFieldName = $rootClass->getSingleIdentifierFieldName();
$pathType = PathExpression::TYPE_STATE_FIELD;
if (isset($parent['metadata']->associationMappings[$identifierFieldName])) {
if (isset($rootClass->associationMappings[$identifierFieldName])) {
$pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
}
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, $identifierFieldName);
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName);
$pathExpression->type = $pathType;
$count = $this->_getQuery()->getHint(self::HINT_PAGINATOR_ID_COUNT);