1
0
mirror of synced 2025-01-25 01:31:40 +03:00

Merge pull request #2 from shustrik/multiplie_tree_walker

Multiplie tree walker bugfix
This commit is contained in:
Strate 2014-02-06 20:04:24 +03:00
commit ffe5bf1fe3
3 changed files with 180 additions and 4 deletions

View File

@ -25,7 +25,7 @@ namespace Doctrine\ORM\Query;
* the AST to influence the final output produced by the last walker.
*
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
* @since 2.0
*/
class TreeWalkerChain implements TreeWalker
{
@ -34,7 +34,7 @@ class TreeWalkerChain implements TreeWalker
*
* @var TreeWalker[]
*/
private $_walkers = array();
private $_walkers;
/**
* The original Query.
@ -89,6 +89,7 @@ class TreeWalkerChain implements TreeWalker
$this->_query = $query;
$this->_parserResult = $parserResult;
$this->_queryComponents = $queryComponents;
$this->_walkers = new TreeWalkerChainIterator($this, $query, $parserResult);
}
/**
@ -100,7 +101,7 @@ class TreeWalkerChain implements TreeWalker
*/
public function addTreeWalker($walkerClass)
{
$this->_walkers[] = new $walkerClass($this->_query, $this->_parserResult, $this->_queryComponents);
$this->_walkers[] = $walkerClass;
}
/**

View File

@ -0,0 +1,126 @@
<?php
/**
* doctrine2
* User: shustrik
* Date: 2/6/14
* Time: 7:03 PM
*/
namespace Doctrine\ORM\Query;
class TreeWalkerChainIterator implements \Iterator, \ArrayAccess
{
/**
* @var TreeWalker[]
*/
private $walkers = array();
/**
* @var TreeWalkerChain
*/
private $treeWalkerChain;
/**
* @var
*/
private $query;
/**
* @var
*/
private $parserResult;
public function __construct(TreeWalkerChain $treeWalkerChain, $query, $parserResult)
{
$this->treeWalkerChain = $treeWalkerChain;
$this->query = $query;
$this->parserResult = $parserResult;
}
/**
* {@inheritdoc}
*/
function rewind()
{
return reset($this->walkers);
}
/**
* {@inheritdoc}
*/
function current()
{
return $this->offsetGet(key($this->walkers));
}
/**
* {@inheritdoc}
*/
function key()
{
return key($this->walkers);
}
/**
* {@inheritdoc}
*/
function next()
{
next($this->walkers);
return $this->offsetGet(key($this->walkers));
}
/**
* {@inheritdoc}
*/
function valid()
{
return key($this->walkers) !== null;
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return isset($this->walkers[$offset]);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
if ($this->offsetExists($offset)) {
return new $this->walkers[$offset](
$this->query,
$this->parserResult,
$this->treeWalkerChain->getQueryComponents()
);
}
return null;
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->walkers[] = $value;
} else {
$this->walkers[$offset] = $value;
}
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->walkers[$offset]);
}
}
}

View File

@ -42,7 +42,7 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase
{
$query = $this->_em->createQuery($dqlToBeTested);
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $treeWalkers)
->useQueryCache(false);
->useQueryCache(false);
if ($outputWalker) {
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, $outputWalker);
@ -96,6 +96,15 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase
__NAMESPACE__ . '\\AddUnknownQueryComponentWalker'
);
}
public function testSupportsSeveralHintsQueries()
{
$this->assertSqlGeneration(
'select u from Doctrine\Tests\Models\CMS\CmsUser u',
"SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.id AS id4, c1_.country AS country5, c1_.zip AS zip6, c1_.city AS city7, c0_.email_id AS email_id8, c1_.user_id AS user_id9 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id WHERE c0_.id = 1",
array('Doctrine\Tests\ORM\Functional\CustomTreeWalkerJoin', 'Doctrine\Tests\ORM\Functional\CustomTreeWalker')
);
}
}
class AddUnknownQueryComponentWalker extends Query\SqlWalker
@ -183,3 +192,43 @@ class CustomTreeWalker extends Query\TreeWalkerAdapter
}
}
}
class CustomTreeWalkerJoin extends Query\TreeWalkerAdapter
{
public function walkSelectStatement(Query\AST\SelectStatement $selectStatement)
{
foreach ($selectStatement->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) {
if ($identificationVariableDeclaration->rangeVariableDeclaration->abstractSchemaName == 'Doctrine\Tests\Models\CMS\CmsUser') {
$identificationVariableDeclaration->joins[] = new Query\AST\Join(
Query\AST\Join::JOIN_TYPE_LEFT,
new Query\AST\JoinAssociationDeclaration(
new Query\AST\JoinAssociationPathExpression(
$identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable,
'address'
),
$identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable . 'a',
null
)
);
$selectStatement->selectClause->selectExpressions[] =
new Query\AST\SelectExpression(
$identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable . 'a',
null,
false
);
$meta1 = $this->_getQuery()->getEntityManager()->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
$meta = $this->_getQuery()->getEntityManager()->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
$this->setQueryComponent($identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable . 'a',
array(
'metadata' => $meta,
'parent' => $identificationVariableDeclaration->rangeVariableDeclaration->aliasIdentificationVariable,
'relation' => $meta1->getAssociationMapping('address'),
'map' => null,
'nestingLevel' => 0,
'token' => null
)
);
}
}
}
}