Symfony2 Doctrine Querybuilder select all

ZvL picture ZvL · Dec 12, 2013 · Viewed 15.3k times · Source

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!

Answer

Cerad picture Cerad · Dec 12, 2013

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.