私は演習を含むプロジェクトに取り組んでおり、各演習には 1 対多の関係で 1 つまたは複数の回答があります。
演習のすべての回答を変数に保存しようとしています。致命的なエラーが表示されます:
致命的なエラー: 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();
}
}