Is it possible to use index by with the doctrine 2 query builder, and if so what is the proper syntax?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With