1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Merge pull request #331 from gedrox/DDC-1757

DDC-1757 test and patched query builder
This commit is contained in:
Benjamin Eberlei 2012-05-04 10:08:59 -07:00
commit 63b2c03a02
2 changed files with 127 additions and 8 deletions

View File

@ -96,6 +96,11 @@ class QueryBuilder
*/
private $_maxResults = null;
/**
* @var array Keeps root entity alias names for join entities.
*/
private $joinRootAliases = array();
/**
* Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>.
*
@ -219,6 +224,32 @@ class QueryBuilder
->setMaxResults($this->_maxResults);
}
/**
* Finds the root entity alias of the joined entity.
*
* @param string $alias The alias of the new join entity
* @param string $parentAlias The parent entity alias of the join relationship
* @return string
*/
private function findRootAlias($alias, $parentAlias)
{
$rootAlias = null;
if (in_array($parentAlias, $this->getRootAliases())) {
$rootAlias = $parentAlias;
} elseif (isset($this->joinRootAliases[$parentAlias])) {
$rootAlias = $this->joinRootAliases[$parentAlias];
} else {
// Should never happen with correct joining order. Might be
// thoughtful to throw exception instead.
$rootAlias = $this->getRootAlias();
}
$this->joinRootAliases[$alias] = $rootAlias;
return $rootAlias;
}
/**
* Gets the FIRST root alias of the query. This is the first entity alias involved
* in the construction of the query.
@ -667,11 +698,9 @@ class QueryBuilder
*/
public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
$rootAlias = substr($join, 0, strpos($join, '.'));
$parentAlias = substr($join, 0, strpos($join, '.'));
if ( ! in_array($rootAlias, $this->getRootAliases())) {
$rootAlias = $this->getRootAlias();
}
$rootAlias = $this->findRootAlias($alias, $parentAlias);
$join = new Expr\Join(
Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition, $indexBy
@ -703,11 +732,9 @@ class QueryBuilder
*/
public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
$rootAlias = substr($join, 0, strpos($join, '.'));
$parentAlias = substr($join, 0, strpos($join, '.'));
if ( ! in_array($rootAlias, $this->getRootAliases())) {
$rootAlias = $this->getRootAlias();
}
$rootAlias = $this->findRootAlias($alias, $parentAlias);
$join = new Expr\Join(
Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition, $indexBy

View File

@ -0,0 +1,92 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\UnitOfWork;
require_once __DIR__ . '/../../../TestInit.php';
class DDC1757Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function testFailingCase()
{
$qb = $this->_em->createQueryBuilder();
/* @var $qb \Doctrine\ORM\QueryBuilder */
$qb->select('_a')
->from(__NAMESPACE__ . '\DDC1757A', '_a')
->from(__NAMESPACE__ . '\DDC1757B', '_b')
->join('_b.c', '_c')
->join('_c.d', '_d');
$q = $qb->getQuery();
$dql = $q->getDQL();
// Show difference between expected and actual queries on error
self::assertEquals("SELECT _a FROM " . __NAMESPACE__ . "\DDC1757A _a, " . __NAMESPACE__ . "\DDC1757B _b INNER JOIN _b.c _c INNER JOIN _c.d _d",
$dql,
"Wrong DQL query");
}
}
/**
* @Entity
*/
class DDC1757A
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $id;
}
/**
* @Entity
*/
class DDC1757B
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @OneToOne(targetEntity="DDC1757C")
*/
private $c;
}
/**
* @Entity
*/
class DDC1757C
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @OneToOne(targetEntity="DDC1757D")
*/
private $d;
}
/**
* @Entity
*/
class DDC1757D
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
public $id;
}