-
-
Save doctrinebot/7b1b19046637f3a9f15e to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-1757 - https://github.com/doctrine/doctrine2/issues/2407
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Doctrine\Tests\ORM\Functional\Ticket; | |
use Doctrine\ORM\UnitOfWork; | |
require_once __DIR__ . '/../../../TestInit.php'; | |
class DDC1757Test extends \PHPUnit_Framework_TestCase | |
{ | |
protected function setUp() | |
{ | |
parent::setUp(); | |
try { | |
$this->_schemaTool->createSchema(array( | |
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1757A'), | |
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1757B'), | |
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1757C'), | |
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1757D'), | |
)); | |
} catch(\Exception $ignored) {} | |
} | |
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(); | |
$q->getResult(); | |
} | |
} | |
/** | |
* @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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/lib/Doctrine/ORM/QueryBuilder.php b/src/lib/Doctrine/ORM/QueryBuilder.php | |
index 5212634..343dcd7 100644 | |
--- a/src/lib/Doctrine/ORM/QueryBuilder.php | |
+++ b/src/lib/Doctrine/ORM/QueryBuilder.php | |
@@ -97,6 +97,12 @@ class QueryBuilder | |
private $_maxResults = null; | |
/** | |
+ * Keeps root entity alias names for join entities | |
+ * @var array | |
+ */ | |
+ private $joinRootAliases = array(); | |
+ | |
+ /** | |
* Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>. | |
* | |
* @param EntityManager $em The EntityManager to use. | |
@@ -234,10 +240,22 @@ class QueryBuilder | |
* @deprecated Please use $qb->getRootAliases() instead. | |
* @return string $rootAlias | |
*/ | |
- public function getRootAlias() | |
+ public function getRootAlias($rootAlias = null, $alias = null) | |
{ | |
- $aliases = $this->getRootAliases(); | |
- return $aliases[0]; | |
+ if ( ! is_null($rootAlias) && in_array($rootAlias, $this->getRootAliases())) { | |
+ // Do nothing | |
+ } elseif ( ! is_null($rootAlias) && isset($this->joinRootAliases[$rootAlias])) { | |
+ $rootAlias = $this->joinRootAliases[$rootAlias]; | |
+ } else { | |
+ $aliases = $this->getRootAliases(); | |
+ $rootAlias = $aliases[0]; | |
+ } | |
+ | |
+ if ( ! is_null($alias)) { | |
+ $this->joinRootAliases[$alias] = $rootAlias; | |
+ } | |
+ | |
+ return $rootAlias; | |
} | |
/** | |
@@ -672,9 +690,7 @@ class QueryBuilder | |
{ | |
$rootAlias = substr($join, 0, strpos($join, '.')); | |
- if ( ! in_array($rootAlias, $this->getRootAliases())) { | |
- $rootAlias = $this->getRootAlias(); | |
- } | |
+ $rootAlias = $this->getRootAlias($rootAlias, $alias); | |
$join = new Expr\Join( | |
Expr\Join::INNER_JOIN, $join, $alias, $conditionType, $condition, $indexBy | |
@@ -708,9 +724,7 @@ class QueryBuilder | |
{ | |
$rootAlias = substr($join, 0, strpos($join, '.')); | |
- if ( ! in_array($rootAlias, $this->getRootAliases())) { | |
- $rootAlias = $this->getRootAlias(); | |
- } | |
+ $rootAlias = $this->getRootAlias($rootAlias, $alias); | |
$join = new Expr\Join( | |
Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition, $indexBy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment