Added programatical support to define indexBy on root aliases.
This commit is contained in:
parent
1369cdda2b
commit
5ab4c3d50c
@ -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.
|
||||
*
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user