0

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!

mylogon
  • 2,772
  • 2
  • 28
  • 42
user1806756
  • 145
  • 1
  • 2
  • 11

2 Answers2

5

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.

lxg
  • 12,375
  • 12
  • 51
  • 73
  • In addition to this solution I'd suggest to do `getResult(AbstractQuery::HYDRATE_SCALAR)`. It makes the last line of the code unnecessary as soon as Doctrine will make the result of `getResult` an array – Stepashka Jan 23 '17 at 13:07
  • @Stepashka Maybe you can explain why you suggest this and what this line exactly does? Only suggesting things without any explanation won't help anybody. – Wilt Jan 23 '17 at 13:08
  • @lxg If certain associations are marked as `fetch="EAGER"` this might still lead to unnecessary joins right? Or not? – Wilt Jan 23 '17 at 13:15
  • @Stepashka: Unfortunately, `AbstractQuery::HYDRATE_SCALAR` doesn’t do what you think it does. You still get an array of arrays with one value each. This is a known problem, see e.g. https://stackoverflow.com/questions/11657835/how-to-get-a-one-dimensional-scalar-array-as-a-doctrine-dql-query-result – lxg Jan 23 '17 at 13:48
  • @Wilt: I don’t know for sure, but don’t think so. If it did, I would consider it to be a bug in Doctrine. – lxg Jan 23 '17 at 13:48
-1

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

Joe Yahchouchi
  • 627
  • 7
  • 16
  • 1
    Native queries are a bad suggestion as they circumvent the DBAL and bind your application to a specific DB product. Native queries are always your last resort, only to be used if everything else fails. – lxg Jan 23 '17 at 11:17
  • Also, what’s the point of `WHERE 1=1`? This only makes sense in crappy applications which extend the SQL statement through obscure filters. (*cough* … WordPress … *cough*) – lxg Jan 23 '17 at 11:27
  • It's to say select all... I'm sorry I use doctrine so much I rarely write sql queries. And since he is not putting any external inputs from the user into this, I don't see why this would be any less secure... – Joe Yahchouchi Jan 23 '17 at 11:48
  • I didn’t say that `WHERE 1=1` is insecure, it’s just nonsense. `SELECT id FROM Foo` would be sufficient. Applications like WordPress add `WHERE 1=1` so you can extend the query through filters without knowing the original query (e.g. `$sql .= " AND field = 'value'"`). – lxg Jan 23 '17 at 11:55
  • Does Wordpress really do that? Another good reason to never use Wordpress. – Wilt Jan 23 '17 at 13:10
  • I never used wordpress. Just for the record. – Joe Yahchouchi Jan 23 '17 at 13:16
  • @Wilt: Just checked, they seem to have removed it recently, you can still see it in not-too-old branches. https://github.com/WordPress/WordPress/blob/4.6-branch/wp-includes/query.php#L3550 (Haven’t used WP for some years now, but when I had to, it made my eyes bleed.) Update: Just checked out the latest WP sources, and still found some results for `grep -r 'WHERE 1=1'`. – lxg Jan 23 '17 at 13:32
  • *"it made my eyes bleed"* – Wilt Jan 23 '17 at 14:15