I have two entities Question and Answer with appropriate relationships.
I want an optimized way (less resource) to find all questions (only id and title) without answers with doctrine.
Thanks for your answer.
I have two entities Question and Answer with appropriate relationships.
I want an optimized way (less resource) to find all questions (only id and title) without answers with doctrine.
Thanks for your answer.
In your controller…
Using the QueryBuilder:
$repository = $this->getDoctrine()->getRepository('AcmeStoreBundle:Question');
$qb = $repository->createQueryBuilder('Question');
$query = $qb
->select('DISTINCT Question.id, Question.title')
->leftJoin('Question.answers', 'Answer', $qb->expr()->isNull('Answer.id'))
->getQuery();
$questions = $query->getArrayResult();
Or DQL (partial objects:
$query = $em->createQuery("select partial Question.{id,title}
from MyApp\Domain\Question Question
left join Question.answers Answer
where Answer.id is NULL");
$questions = $query->getResult();
The query builder will return an array while the DQL will return partial objects.
See: