Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 query builder and index by

Tags:

doctrine-orm

Is it possible to use index by with the doctrine 2 query builder, and if so what is the proper syntax?

like image 402
John Avatar asked Dec 13 '11 19:12

John


2 Answers

Beginning with 2.2, you can now include INDEX BY in your from statement. If you are adding a from clause,

$qb->from($class, $alias, $indexBy);

If you already have a from clause that you want to replace, then you can replace it with:

$qb->add('from', new Expr\From($class, $alias, $indexBy), false);

There's an open pull request to add it to the Repository's createQueryBuilder function as well, so hopefully this will get added soon.

like image 158
cmenning Avatar answered Sep 22 '22 13:09

cmenning


For an update. You can do something like this.

$qb = $entityManager->createQueryBuilder();
$qb->from($repository->getClassName(), 'a', 'a.id');
$qb->select(a);

$result = new ArrayCollection($qb->getQuery()->getResult());

As a result, array collection will contain properly indexed elements.

like image 32
ken Avatar answered Sep 19 '22 13:09

ken