Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request doctrine#1313 from doctrine/querybuilder-indexby
Browse files Browse the repository at this point in the history
Added programmatical support to define indexBy on root aliases.
  • Loading branch information
guilhermeblanco committed Feb 25, 2015
2 parents 1369cdd + 5ab4c3d commit 7fc1dc9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/Doctrine/ORM/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,50 @@ public function from($from, $alias, $indexBy = null)
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.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/ORM/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ public function testSimpleDelete()
$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()
Expand Down Expand Up @@ -184,6 +203,17 @@ public function testMultipleFrom()
$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()
Expand Down

0 comments on commit 7fc1dc9

Please sign in to comment.