1
0
mirror of synced 2024-12-14 15:16:04 +03:00

Merge branch 'DDC-1685'

This commit is contained in:
Benjamin Eberlei 2012-05-27 18:33:46 +02:00
commit 47ac61b3e0
6 changed files with 126 additions and 29 deletions

View File

@ -87,18 +87,28 @@ class CountOutputWalker extends SqlWalker
throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
} }
$rootClass = $from[0]->rangeVariableDeclaration->abstractSchemaName; $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable;
$rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $this->queryComponents[$rootAlias]['metadata'];
$rootIdentifier = $rootClass->identifier;
// Get the identity properties from the metadata
$rootIdentifier = $this->queryComponents[$rootAlias]['metadata']->identifier;
// For every identifier, find out the SQL alias by combing through the ResultSetMapping // For every identifier, find out the SQL alias by combing through the ResultSetMapping
$sqlIdentifier = array(); $sqlIdentifier = array();
foreach ($rootIdentifier as $property) { foreach ($rootIdentifier as $property) {
foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) { if (isset($rootClass->fieldMappings[$property])) {
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) { foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) {
$sqlIdentifier[$property] = $alias; if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
$sqlIdentifier[$property] = $alias;
}
}
}
if (isset($rootClass->associationMappings[$property])) {
$joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name'];
foreach (array_keys($this->rsm->metaMappings, $joinColumn) as $alias) {
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
$sqlIdentifier[$property] = $alias;
}
} }
} }
} }

View File

@ -60,15 +60,21 @@ class CountWalker extends TreeWalkerAdapter
if (count($rootComponents) > 1) { if (count($rootComponents) > 1) {
throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
} }
$root = reset($rootComponents); $root = reset($rootComponents);
$parentName = key($root); $parentName = key($root);
$parent = current($root); $parent = current($root);
$identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName();
$pathType = PathExpression::TYPE_STATE_FIELD;
if (isset($parent['metadata']->associationMappings[$identifierFieldName])) {
$pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
}
$pathExpression = new PathExpression( $pathExpression = new PathExpression(
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName,
$parent['metadata']->getSingleIdentifierFieldName() $identifierFieldName
); );
$pathExpression->type = PathExpression::TYPE_STATE_FIELD; $pathExpression->type = $pathType;
$distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT); $distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT);
$AST->selectClause->selectExpressions = array( $AST->selectClause->selectExpressions = array(

View File

@ -98,19 +98,28 @@ class LimitSubqueryOutputWalker extends SqlWalker
throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
} }
$rootClass = $from[0]->rangeVariableDeclaration->abstractSchemaName; $rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable;
$rootAlias = $from[0]->rangeVariableDeclaration->aliasIdentificationVariable; $rootClass = $this->queryComponents[$rootAlias]['metadata'];
$rootIdentifier = $rootClass->identifier;
// Get the identity properties from the metadata
$metadata = $this->queryComponents[$rootAlias]['metadata'];
$rootIdentifier = $metadata->identifier;
// For every identifier, find out the SQL alias by combing through the ResultSetMapping // For every identifier, find out the SQL alias by combing through the ResultSetMapping
$sqlIdentifier = array(); $sqlIdentifier = array();
foreach ($rootIdentifier as $property) { foreach ($rootIdentifier as $property) {
foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) { if (isset($rootClass->fieldMappings[$property])) {
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) { foreach (array_keys($this->rsm->fieldMappings, $property) as $alias) {
$sqlIdentifier[$property] = $alias; if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
$sqlIdentifier[$property] = $alias;
}
}
}
if (isset($rootClass->associationMappings[$property])) {
$joinColumn = $rootClass->associationMappings[$property]['joinColumns'][0]['name'];
foreach (array_keys($this->rsm->metaMappings, $joinColumn) as $alias) {
if ($this->rsm->columnOwnerMap[$alias] == $rootAlias) {
$sqlIdentifier[$property] = $alias;
}
} }
} }
} }

View File

@ -74,6 +74,10 @@ class LimitSubqueryWalker extends TreeWalkerAdapter
} }
$identifier = $parent['metadata']->getSingleIdentifierFieldName(); $identifier = $parent['metadata']->getSingleIdentifierFieldName();
if (isset($parent['metadata']->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( $this->_getQuery()->setHint(
self::IDENTIFIER_TYPE, self::IDENTIFIER_TYPE,
Type::getType($parent['metadata']->getTypeOfField($identifier)) Type::getType($parent['metadata']->getTypeOfField($identifier))

View File

@ -81,14 +81,18 @@ class WhereInWalker extends TreeWalkerAdapter
if (count($rootComponents) > 1) { if (count($rootComponents) > 1) {
throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction"); throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
} }
$root = reset($rootComponents); $root = reset($rootComponents);
$parentName = key($root); $parentName = key($root);
$parent = current($root); $parent = current($root);
$identifierFieldName = $parent['metadata']->getSingleIdentifierFieldName();
$pathExpression = new PathExpression( $pathType = PathExpression::TYPE_STATE_FIELD;
PathExpression::TYPE_STATE_FIELD, $parentName, $parent['metadata']->getSingleIdentifierFieldName() if (isset($parent['metadata']->associationMappings[$identifierFieldName])) {
); $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
$pathExpression->type = PathExpression::TYPE_STATE_FIELD; }
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, $identifierFieldName);
$pathExpression->type = $pathType;
$count = $this->_getQuery()->getHint(self::HINT_PAGINATOR_ID_COUNT); $count = $this->_getQuery()->getHint(self::HINT_PAGINATOR_ID_COUNT);

View File

@ -0,0 +1,64 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
use Doctrine\Tests\Models\DDC117\DDC117Article;
use Doctrine\ORM\Tools\Pagination\Paginator;
/**
* @group DDC-1685
*/
class DDC1685Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $paginator;
protected function setUp()
{
$this->useModelSet('ddc117');
parent::setUp();
$this->_em->createQuery('DELETE FROM Doctrine\Tests\Models\DDC117\DDC117ArticleDetails ad')->execute();
$article = new DDC117Article("Foo");
$this->_em->persist($article);
$this->_em->flush();
$articleDetails = new DDC117ArticleDetails($article, "Very long text");
$this->_em->persist($articleDetails);
$this->_em->flush();
$dql = "SELECT ad FROM Doctrine\Tests\Models\DDC117\DDC117ArticleDetails ad";
$query = $this->_em->createQuery($dql);
$this->paginator = new Paginator($query);
}
public function testPaginateCount()
{
$this->assertEquals(1, count($this->paginator));
}
public function testPaginateIterate()
{
foreach ($this->paginator as $ad) {
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails', $ad);
}
}
public function testPaginateCountNoOutputWalkers()
{
$this->paginator->setUseOutputWalkers(false);
$this->assertEquals(1, count($this->paginator));
}
public function testPaginateIterateNoOutputWalkers()
{
$this->paginator->setUseOutputWalkers(false);
$this->setExpectedException('RuntimeException', 'Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator.');
foreach ($this->paginator as $ad) {
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails', $ad);
}
}
}