user3810730
user3810730

Reputation:

Symfony query builder and aliases

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 Joining Related Records in Symfony 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 and in my controller. At the moment I am getting a single answer result instead of all the answers that are related to my question.

public function findOneByIdJoinedToCategory($questionId)
    {
        $query = $this->getEntityManager()
            ->createQuery(
                'SELECT a, q FROM QuizBundle:Answer a 
                 JOIN a.answers q 
                 WHERE a.id = :id'
            )->setParameter('id', $questionId);

        try {
            return $query->getOneOrNullResult();
        } catch (\Doctrine\ORM\NoResultException $e) {
            return null;
        }
    }

public function showAction($answerId)
    {
        $product = $this->getDoctrine()
            ->getRepository('QuizBundle:Answer')
            ->findOneByIdJoinedToCategory($answerId);

        $data = $product->getAnswer();

        return new Response($data);

        // ...
    }

Relation between my entities:

class Answer
{

    /**
     * @ORM\ManyToOne(targetEntity="Question", inversedBy="answers")
     * @ORM\JoinColumn(name="question_id", referencedColumnName="id")
     */
    private $question;

public function setQuestion(\QuizBundle\Entity\Question $question = null)
    {
        $this->question = $question;
        return $this;
    }
    /**
     * Get question
     *
     * @return \QuizBundle\Entity\Question
     */
    public function getQuestion()
    {
        return $this->question;
    }
}


 class Question
{
    /**
     * @ORM\OneToMany(targetEntity="Answer", mappedBy="question")
     */
    private $answers;
    public function __construct()
    {
        $this->answers = new ArrayCollection();
    }

    public function addAnswer(\QuizBundle\Entity\Answer $answer)
    {
        $this->answers[] = $answer;
        return $this;
    }

    /**
     * Remove answer
     *
     * @param \QuizBundle\Entity\Answer $answer
     */

    public function removeAnswer(\QuizBundle\Entity\Answer $answer)
    {
        $this->answers->removeElement($answer);
    }

    /**
     * Get answers
     *
     * @return \Doctrine\Common\Collections\Collection
     */

    public function getAnswers()
    {
        return $this->answers;
    }

  }

Upvotes: 1

Views: 2349

Answers (1)

marc
marc

Reputation: 330

Your question refers more to Doctrine than Symfony. You should get a look at the Doctrine's documentation related to Association Mapping.

There are multiple forms of OneToMany relationships. What you probably want here is to have a bidirectional OneToMany relation between Question and Answer. That way, you can make a request like :

SELECT q, a FROM QuizBundle:Question q JOIN q.answers a WHERE q.id = :id

Upvotes: 1

Related Questions