Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/7b1b19046637f3a9f15e to your computer and use it in GitHub Desktop.
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
<?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;
}
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