0

I'm working on a project that has exercises, and each exercise has one or multiple answers in a relationship of one-to-many.

I'm trying to save in a variable all the answers of an exercise. A fatal error appears:

Fatal error: Call to a member function getAnswers() on a non-object in C:\wamp\www\backtoschool\prezentari.dev\src\Prezentari\PrezentariBundle\Controller\PageController.php on line 78

The code is as follows:

public function exerciseAction($lessonid, $firstex, $exerciseid)
{
    $em = $this->getDoctrine()->getEntityManager();

    $exercise = $em->getRepository('PrezentariPrezentariBundle:Exercise')->findByLesson($lessonid);

    $answer = $exercise->getAnswers();

    if ($firstex == 'default') {
        $firstex = 0;
    }
    elseif (isset($exercise[$firstex+1])) {
        $firstex = $firstex + 1;
    }
    else {
        $firstex = 0;
    }
    return $this->render('PrezentariPrezentariBundle:Page:exercise.html.twig', array(
        'exercise' => $exercise[$firstex],
        'lessonid' => $lessonid,
        'firstex' => $firstex,
        'exerciseid' => $exerciseid,
    ));
}

The entity:

<?php

namespace Prezentari\PrezentariBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Prezentari\PrezentariBundle\Entity\Exercise
 */
class Exercise
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var integer $chapterId
     */
    private $lessonId;

    /**
     * @var integer $type
     */
    private $type;

    /**
     * @var string $description
     */
    private $description;

    /**
     * @var string $image
     */
    private $image;

    /**
     * @var integer $nrex
     */
    private $nrex;

    /**
     * @var date $creationDate
     */
    private $creationDate;

    /**
     * @var integer $hidden
     */
    private $hidden;

    /**
     * @var integer $deleted
     */
    private $deleted;

    /**
     * @var Prezentari\PrezentariBundle\Entity\Lesson
     */
    private $lesson;
    /**
     * @var Prezentari\PrezentariBundle\Entity\Exercise
     */
    private $answers;

    public function __construct()
    {
        $this->answers = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get lessonId
     *
     * @return integer
     */
    public function getLessonId()
    {
        return $this->lessonId;
    }

    /**
     * Set type
     *
     * @param integer $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * Get type
     *
     * @return integer 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set description
     *
     * @param string $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set image
     *
     * @param string $image
     */
    public function setImage($image)
    {
        $this->image = $image;
    }

    /**
     * Get image
     *
     * @return string 
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set nrex
     *
     * @param integer $nrex
     */
    public function setNrex($nrex)
    {
        $this->nrex = $nrex;
    }

    /**
     * Get nrex
     *
     * @return integer 
     */
    public function getNrex()
    {
        return $this->nrex;
    }

    /**
     * Set creationDate
     *
     * @param date $creationDate
     */
    public function setCreationDate($creationDate)
    {
        $this->creationDate = $creationDate;
    }

    /**
     * Get creationDate
     *
     * @return date 
     */
    public function getCreationDate()
    {
        return $this->creationDate;
    }

    /**
     * Set hidden
     *
     * @param integer $hidden
     */
    public function setHidden($hidden)
    {
        $this->hidden = $hidden;
    }

    /**
     * Get hidden
     *
     * @return integer 
     */
    public function getHidden()
    {
        return $this->hidden;
    }

    /**
     * Set deleted
     *
     * @param integer $deleted
     */
    public function setDeleted($deleted)
    {
        $this->deleted = $deleted;
    }

    /**
     * Get deleted
     *
     * @return integer 
     */
    public function getDeleted()
    {
        return $this->deleted;
    }

    /**
     * Add answers
     *
     * @param Prezentari\PrezentariBundle\Entity\Answer $answers
     */
    public function addAnswer(\Prezentari\PrezentariBundle\Entity\Answer $answers)
    {
        $this->answers[] = $answers;
    }

    /**
     * Get exercises
     *
     * @return Doctrine\Common\Collections\Collection
     */
    public function getAnswers()
    {
        return $this->answers;
    }

    /**
     * Set lesson
     *
     * @param Prezentari\PrezentariBundle\Entity\Lesson $lesson
     */
    public function setLesson(\Prezentari\PrezentariBundle\Entity\Lesson $lesson)
    {
        $this->lesson = $lesson;
    }

    /**
     * Get lesson
     *
     * @return Prezentari\PrezentariBundle\Entity\Lesson 
     */
    public function getLesson()
    {
        return $this->lesson;
    }

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

0 回答 0