Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Doctrine Querybuilder select all

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!

like image 874
ZvL Avatar asked Dec 12 '13 15:12

ZvL


1 Answers

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.

like image 52
Cerad Avatar answered Sep 27 '22 21:09

Cerad