Reputation: 145
I was wondering if it is possible to modify $this->getDoctrine()->getRepository('AppBundle:Foo')->findAll()
, so that I only get the IDs of the related entities. Because Foo is related to 'one User' and 'multiple Groups', I always get the 'whole User object' and 'ALL Group objects' in the result, which makes the result very unclear. So, is it possible to only print the IDs of the related objects?
I hope someone can help me. Thanks!
Upvotes: 0
Views: 1396
Reputation: 626
You will have to write your own custom query:
$query = $this->getDoctrine()->getManager()->createNativeQuery('SELECT id FROM foo');
$foos= $query->getResult();
the above should work see here for more info http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html
Upvotes: -1
Reputation: 13117
You don’t have to retrieve the full entities, you can as well select only the fields you need. Instead of an entity list, you will get a list of plain arrays, where each array contains the selected fields.
$ids = $em->createQueryBuilder() // $em is your entity manager
->select("foo.id")
->from("AppBundle:Foo", "foo")
->getQuery()->getResult();
$ids = array_map("current", $ids);
Note: The last line is optional, it will “flatten” your array when you select only one field.
Upvotes: 5