我正在开展一个有练习的项目,每个练习在一对多的关系中有一个或多个答案。
我正在尝试将变量的所有答案保存在变量中。出现致命错误:
致命错误:在第78行的C:\ wamp \ www \ backtoschool \ prezentari.dev \ src \ Prezentari \ PrezentariBundle \ Controller \ PageController.php中的非对象上调用成员函数getAnswers()
代码如下:
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,
));
}
实体:
<?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();
}
}