Thanks to visit codestin.com
Credit goes to github.com

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

Commit 5ab4c3d

Browse files
Added programatical support to define indexBy on root aliases.
1 parent 1369cdd commit 5ab4c3d

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

lib/Doctrine/ORM/QueryBuilder.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,50 @@ public function from($from, $alias, $indexBy = null)
881881
return $this->add('from', new Expr\From($from, $alias, $indexBy), true);
882882
}
883883

884+
/**
885+
* Updates a query root corresponding to an entity setting its index by. This method is intended to be used with
886+
* EntityRepository->createQueryBuilder(), which creates the initial FROM clause and do not allow you to update it
887+
* setting an index by.
888+
*
889+
* <code>
890+
* $qb = $userRepository->createQueryBuilder('u')
891+
* ->indexBy('u', 'u.id');
892+
*
893+
* // Is equivalent to...
894+
*
895+
* $qb = $em->createQueryBuilder()
896+
* ->select('u')
897+
* ->from('User', 'u', 'u.id');
898+
* </code>
899+
*
900+
* @param string $alias The root alias of the class.
901+
* @param string $indexBy The index for the from.
902+
*
903+
* @return QueryBuilder This QueryBuilder instance.
904+
*
905+
* @throws Query\QueryException
906+
*/
907+
public function indexBy($alias, $indexBy)
908+
{
909+
$rootAliases = $this->getRootAliases();
910+
911+
if (!in_array($alias, $rootAliases)) {
912+
throw new Query\QueryException(
913+
sprintf('Specified root alias %s must be set before invoking indexBy().', $alias)
914+
);
915+
}
916+
917+
foreach ($this->_dqlParts['from'] as &$fromClause) {
918+
if ($fromClause->getAlias() !== $alias) {
919+
continue;
920+
}
921+
922+
$fromClause = new Expr\From($fromClause->getFrom(), $fromClause->getAlias(), $indexBy);
923+
}
924+
925+
return $this;
926+
}
927+
884928
/**
885929
* Creates and adds a join over an entity association to the query.
886930
*

tests/Doctrine/Tests/ORM/QueryBuilderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ public function testSimpleDelete()
109109
$this->assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
110110
}
111111

112+
public function testSimpleSelectWithFromIndexBy()
113+
{
114+
$qb = $this->_em->createQueryBuilder()
115+
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u', 'u.id')
116+
->select('u.id', 'u.username');
117+
118+
$this->assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
119+
}
120+
121+
public function testSimpleSelectWithIndexBy()
122+
{
123+
$qb = $this->_em->createQueryBuilder()
124+
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
125+
->indexBy('u', 'u.id')
126+
->select('u.id', 'u.username');
127+
128+
$this->assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
129+
}
130+
112131
public function testSimpleUpdate()
113132
{
114133
$qb = $this->_em->createQueryBuilder()
@@ -184,6 +203,17 @@ public function testMultipleFrom()
184203
$this->assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g');
185204
}
186205

206+
public function testMultipleFromWithIndexBy()
207+
{
208+
$qb = $this->_em->createQueryBuilder()
209+
->select('u', 'g')
210+
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
211+
->from('Doctrine\Tests\Models\CMS\CmsGroup', 'g')
212+
->indexBy('g', 'g.id');
213+
214+
$this->assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g INDEX BY g.id');
215+
}
216+
187217
public function testMultipleFromWithJoin()
188218
{
189219
$qb = $this->_em->createQueryBuilder()

0 commit comments

Comments
 (0)