From 85d40847acc053c0609735d2b9953ada342be189 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 15 May 2011 22:11:10 +0200 Subject: [PATCH] Reintroduce QueryBuilder::getRootAlias() for backwards compatibility reasons, mark as @deprecated --- lib/Doctrine/ORM/QueryBuilder.php | 21 ++++++++++++++ tests/Doctrine/Tests/ORM/QueryBuilderTest.php | 29 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index fce4731c5..c0df0e4c2 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -217,6 +217,27 @@ class QueryBuilder ->setFirstResult($this->_firstResult) ->setMaxResults($this->_maxResults); } + + /** + * Gets the FIRST root alias of the query. This is the first entity alias involved + * in the construction of the query. + * + * + * $qb = $em->createQueryBuilder() + * ->select('u') + * ->from('User', 'u'); + * + * echo $qb->getRootAlias(); // u + * + * + * @deprecated Please use $qb->getRootAliases() instead. + * @return string $rootAlias + */ + public function getRootAlias() + { + $aliases = $this->getRootAliases(); + return $aliases[0]; + } /** * Gets the root alias of the query. This is the first entity alias involved diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 119a40dcd..b44f8578a 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -633,4 +633,33 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals(2, $expr->count(), "Modifying the second query should affect the first one."); } + + public function testGetRootAlias() + { + $qb = $this->_em->createQueryBuilder() + ->select('u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u'); + + $this->assertEquals('u', $qb->getRootAlias()); + } + + public function testGetRootAliases() + { + $qb = $this->_em->createQueryBuilder() + ->select('u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u'); + + $this->assertEquals(array('u'), $qb->getRootAliases()); + } + + public function testGetSeveralRootAliases() + { + $qb = $this->_em->createQueryBuilder() + ->select('u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u2'); + + $this->assertEquals(array('u', 'u2'), $qb->getRootAliases()); + $this->assertEquals('u', $qb->getRootAlias()); + } } \ No newline at end of file