Symfony2 Doctrine Querybuilder select all
Asked Answered
A

1

7

I'm currently working on a Service in SF2 that queries the database with the QueryBuilder using a class variable set with a repository-specific QueryBuilder in the constructor of this Service. Which means i would like to make use of this set QueryBuilder as much as possible for neater code and a clean feeling using it.

I want to avoid creating a query on the EntityManager, but instead solely query using this predefined Querybuilder.

I'm looking for something that would look/work like the following:

$query = $this->fooRepository->createQueryBuilder('f')->select('*');
return $query->getResult(Query::HYDRATE_ARRAY);

The above would (if it worked) return all the foo in the database as far as i know..

If you think i'm being stupid and should do something different in regard to the predefined QueryBuilders or just use the:

createQuery()

method because it simply isn't good practice or impossible, don't hesitate to tell me.

Thanks!

Accelerate answered 12/12, 2013 at 15:43 Comment(0)
C
9

Try:

$qb = $this->fooRepository->createQueryBuilder('foo');
return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);

No need for a select(*). All the foo items will be selected since no where clauses were added.

Cathleencathlene answered 12/12, 2013 at 16:19 Comment(2)
What about the query that includes multiple joins and all columns from those join tables are also needed. Any solution to that?Salted
Try it. You might be surprised.Cathleencathlene

© 2022 - 2024 — McMap. All rights reserved.