1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Merge pull request #1313 from doctrine/querybuilder-indexby

Added programmatical support to define indexBy on root aliases.
This commit is contained in:
Guilherme Blanco 2015-02-25 14:42:13 -05:00
commit 7fc1dc9209
2 changed files with 74 additions and 0 deletions

View File

@ -881,6 +881,50 @@ class QueryBuilder
return $this->add('from', new Expr\From($from, $alias, $indexBy), true);
}
/**
* Updates a query root corresponding to an entity setting its index by. This method is intended to be used with
* EntityRepository->createQueryBuilder(), which creates the initial FROM clause and do not allow you to update it
* setting an index by.
*
* <code>
* $qb = $userRepository->createQueryBuilder('u')
* ->indexBy('u', 'u.id');
*
* // Is equivalent to...
*
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->from('User', 'u', 'u.id');
* </code>
*
* @param string $alias The root alias of the class.
* @param string $indexBy The index for the from.
*
* @return QueryBuilder This QueryBuilder instance.
*
* @throws Query\QueryException
*/
public function indexBy($alias, $indexBy)
{
$rootAliases = $this->getRootAliases();
if (!in_array($alias, $rootAliases)) {
throw new Query\QueryException(
sprintf('Specified root alias %s must be set before invoking indexBy().', $alias)
);
}
foreach ($this->_dqlParts['from'] as &$fromClause) {
if ($fromClause->getAlias() !== $alias) {
continue;
}
$fromClause = new Expr\From($fromClause->getFrom(), $fromClause->getAlias(), $indexBy);
}
return $this;
}
/**
* Creates and adds a join over an entity association to the query.
*

View File

@ -109,6 +109,25 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
$this->assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
}
public function testSimpleSelectWithFromIndexBy()
{
$qb = $this->_em->createQueryBuilder()
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u', 'u.id')
->select('u.id', 'u.username');
$this->assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
}
public function testSimpleSelectWithIndexBy()
{
$qb = $this->_em->createQueryBuilder()
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->indexBy('u', 'u.id')
->select('u.id', 'u.username');
$this->assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
}
public function testSimpleUpdate()
{
$qb = $this->_em->createQueryBuilder()
@ -184,6 +203,17 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
$this->assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g');
}
public function testMultipleFromWithIndexBy()
{
$qb = $this->_em->createQueryBuilder()
->select('u', 'g')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->from('Doctrine\Tests\Models\CMS\CmsGroup', 'g')
->indexBy('g', 'g.id');
$this->assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g INDEX BY g.id');
}
public function testMultipleFromWithJoin()
{
$qb = $this->_em->createQueryBuilder()