1

I'm using symfony 2.0.10 to build a site and in my controller I'm querying a database table using this code:

$query = $this->getDoctrine()->getRepository('MyBundle:MyTable')->createQueryBuilder('x')->getQuery();

$data = $query->getResult();

I would get the results indexed in the array not by sequential numbers (0 to N) but instead by a specified field. I saw that I can do this specifing a INDEX BY in the CreateQuery method but how can I do this with the query builder? In this question a reply is given but the user is not using a repository.

EDIT 1

$query = $this->getDoctrine()->getEntityManager()->createQueryBuilder()->select('x')->from('MyBundle:MyTable', 'x', 'x.myIndexField')->getQuery();
Community
  • 1
  • 1
Stefano
  • 3,213
  • 9
  • 60
  • 101

1 Answers1

1

I haven't tried this but I'm sure that method join has signature:

public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null)

Last parameter is $indexBy, so I guess that is what you're looking for ;)

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • I don't want to join something in the SQL query, I only want to get the result as an array indexed by a field. With the CreateQuery command it's so easy but with QueryBuilder seems more difficult – Stefano Feb 09 '12 at 22:43
  • It seems that it has been fixed in the repository. Take a look at this issue report: http://www.doctrine-project.org/jira/browse/DDC-1335 You might want to update Doctrine. – Jovan Perovic Feb 09 '12 at 22:53
  • I'm using doctrine with symfony and the repository, so as you can see I don't use the form method. Any other idea? – Stefano Feb 09 '12 at 22:59
  • I'm pretty sure that was a typo. :) If you take a look at github change log here: https://github.com/doctrine/doctrine2/commit/80284a273ded303c059b013808d1199d8546d359 you can see that now there is a `__constructor` of `From` class that supports `$indexBy` ;) – Jovan Perovic Feb 09 '12 at 23:03
  • I saw that constructor bus as I said and as you can see I'm not using the form method because getRepository of symfony seems to alredy do internally something like select * from – Stefano Feb 09 '12 at 23:06
  • Aaahh yeah, there's that! Sorry for the misunderstanding. You could achieve this by creating separate entity repository class and creating an empty QB within some method using: `$this->getEntityManager()->createQueryBuilder()` which has no internal side-effects.... otherwise, I'm not sure how.... – Jovan Perovic Feb 09 '12 at 23:16
  • so because at the moment I'm not using any method of the repository class I switched to the normal getEntityManager()->createQueryBuilder way and I can execute correctly the query as before but it doesn't return me the result indexed by the key I specified. I updated my answer with the new code – Stefano Feb 10 '12 at 14:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7533/discussion-between-jperovic-and-stefano) – Jovan Perovic Feb 10 '12 at 14:31
  • I think there is no need, when you download the latest version of symfony vendors seems to not be updated. Doctrine is still an old version and doesn't include the fix for the indexBy param. This is why it doesn't work. I will try to update the vendors and let you know if it works – Stefano Feb 10 '12 at 14:52
  • Sorry this was an automated message (I think) :D – Jovan Perovic Feb 10 '12 at 16:35
  • mmm I updated symfony vendors to their latest version but somehow it seems to don't update all. For example, I still have old doctrine files (and still don't have the latest From.php file with the indexBy param support) – Stefano Feb 10 '12 at 17:02