In Symfony I have two entities(Question, Answer) that are related to each other. The relation between the two entities is ONE to MANY(one question can have many answers) Following Symfony documentation on relationships between two entities I am using a question_id field that was generated by Symfony in the answer entity. Using a query in my question entity repository I am trying to get a question with all it’s answers, but following the documentation on how to join related records I can’t understand how aliases work in Symfony. Would really appreciate some help in understanding aliases. Thanks This is what I have in my question repository class:
public function findOneByIdJoinedToCategory($questionId)
{
$query = $this->getEntityManager()
->createQuery(
'SELECT a, q FROM QuizBundle:Answer a
JOIN a.question q
WHERE a.id = :id'
)->setParameter('id', $questionId);
try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}