ZvL
ZvL

Reputation: 803

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!

Upvotes: 7

Views: 18478

Answers (1)

Cerad
Cerad

Reputation: 48865

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.

Upvotes: 9

Related Questions