我是 Symfony 2 的新手,我不得不说我仍然没有完全了解它,这就是我想要实现的目标。在“测验”页面上,管理员添加一个新测验,因为他可以使用 jQuery 和嵌套表单“问题”动态添加任意数量的新问题。然后正在创建第二个级别,对于他创建的每个问题,他都可以创建答案,并选择一个或多个作为正确答案,基本上给我留下了多项选择题。
除了数据持久性T_T,我设法让一切正常工作
ErrorHandler ->handle ('2', 'spl_object_hash() expects parameter 1 to be object, array given',
'C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php', '1367', array(**'entity'** => array('qContent' => 'Test QuestionContent', 'qType' => '2', 'answer' => array(array('aContent' => 'Test AswerContent', 'isRight' => '1'))), 'assume' => '2'))
我知道 Doctrine 想要一个实体而不是一个数组,但是我怎样才能持久化我的表单返回的内容而不必完全重建数据持久化过程?!
好的,你已经等得够久了,这里是代码^^
添加测验控制器:
public function addQuizAction($lesson)
{
$new_quiz = new Quiz;
//Find the lesson entity
$repository = $this ->getDoctrine()
->getManager()
->getRepository('InkyCourseBundle:Lesson');
$lessonRep = $repository->findOneBy(array('id' => $lesson));
// Create Quiz
if ($this->getUser()) { $new_quiz->setUser($this->getUser());}
if ($lessonRep) { $new_quiz->setLesson($lessonRep);}
$form = $this->createForm(new QuizType(), $new_quiz);
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$form->bind($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($new_quiz);
$em->flush();
$this->get('session')->getFlashBag()->add('info', 'Quiz bien ajouté');
return $this->redirect($this->generateUrl('quiz_edit', array('id' => $new_quiz->getId(),'lesson' => $lesson)));
}
}
回答实体:
namespace Inky\QuizBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Answer
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Inky\QuizBundle\Entity\AnswerRepository")
*/
class Answer
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="aContent", type="string", length=255)
*/
private $aContent;
/**
* @var boolean
*
* @ORM\Column(name="isRight", type="boolean")
*/
private $isRight;
/**
* @ORM\ManyToOne(targetEntity="Question", inversedBy="Answer")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $question;
/**
* @ORM\ManyToOne(targetEntity="Inky\UserBundle\Entity\User")
*/
private $user;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set aContent
*
* @param string $aContent
* @return Answer
*/
public function setAContent($aContent)
{
$this->aContent = $aContent;
return $this;
}
/**
* Get aContent
*
* @return string
*/
public function getAContent()
{
return $this->aContent;
}
/**
* Set isRight
*
* @param boolean $isRight
* @return Answer
*/
public function setIsRight($isRight)
{
$this->isRight = $isRight;
return $this;
}
/**
* Get isRight
*
* @return boolean
*/
public function getIsRight()
{
return $this->isRight;
}
/**
* Set question
*
* @param \Inky\QuizBundle\Entity\Question $question
* @return Answer
*/
public function setQuestion(\Inky\QuizBundle\Entity\Question $question = null)
{
$this->question = $question;
return $this;
}
/**
* Get question
*
* @return \Inky\QuizBundle\Entity\Question
*/
public function getQuestion()
{
return $this->question;
}
}
问题实体:
<?php
namespace Inky\QuizBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Question
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Inky\QuizBundle\Entity\QuestionRepository")
*/
class Question
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="qContent", type="text")
*/
private $qContent;
/**
* @var integer
*
* @ORM\Column(name="qType", type="smallint")
*/
private $qType;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity="Quiz", inversedBy="Question")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $quiz;
/**
* @ORM\Id
* @ORM\OneToMany(targetEntity="Answer", mappedBy="Question")
*/
private $answer;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set qContent
*
* @param string $qContent
* @return Question
*/
public function setQContent($qContent)
{
$this->qContent = $qContent;
return $this;
}
/**
* Get qContent
*
* @return string
*/
public function getQContent()
{
return $this->qContent;
}
/**
* Set qType
*
* @param integer $qType
* @return Question
*/
public function setQType($qType)
{
$this->qType = $qType;
return $this;
}
/**
* Get qType
*
* @return integer
*/
public function getQType()
{
return $this->qType;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return Question
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return Question
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Constructor
*/
public function __construct()
{
$this->answer = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set question
*
* @param \Inky\QuizBundle\Quiz $question
* @return Question
*/
public function setQuestion(\Inky\QuizBundle\Quiz $question = null)
{
$this->question = $question;
return $this;
}
/**
* Get question
*
* @return \Inky\QuizBundle\Quiz
*/
public function getQuestion()
{
return $this->question;
}
/**
* Add answer
*
* @param \Inky\QuizBundle\Answer $answer
* @return Question
*/
public function addAnswer(\Inky\QuizBundle\Answer $answer)
{
$this->answer[] = $answer;
return $this;
}
/**
* Remove answer
*
* @param \Inky\QuizBundle\Answer $answer
*/
public function removeAnswer(\Inky\QuizBundle\Answer $answer)
{
$this->answer->removeElement($answer);
}
/**
* Get answer
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAnswer()
{
return $this->answer;
}
/**
* Set quiz
*
* @param \Inky\QuizBundle\Quiz $quiz
* @return Question
*/
public function setQuiz(\Inky\QuizBundle\Quiz $quiz = null)
{
$this->quiz = $quiz;
return $this;
}
/**
* Get quiz
*
* @return \Inky\QuizBundle\Quiz
*/
public function getQuiz()
{
return $this->quiz;
}
}
测验实体:
<?php
namespace Inky\QuizBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Quiz
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Inky\QuizBundle\Entity\QuizRepository")
*/
class Quiz
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;
/**
* @var boolean
*
* @ORM\Column(name="isPublic", type="boolean")
*/
private $isPublic;
/**
* @var boolean
*
* @ORM\Column(name="isActive", type="boolean")
*/
private $isActive;
/**
* @ORM\OneToMany(targetEntity="Inky\QuizBundle\Entity\Question", mappedBy="Quiz")
*/
private $question;
/**
* @ORM\ManyToOne(targetEntity="Inky\CourseBundle\Entity\Lesson")
*/
private $lesson;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Quiz
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set descritpion
*
* @param string $descritpion
* @return Quiz
*/
public function setDescritpion($descritpion)
{
$this->descritpion = $descritpion;
return $this;
}
/**
* Get descritpion
*
* @return string
*/
public function getDescritpion()
{
return $this->descritpion;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return Quiz
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return Quiz
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set isPublic
*
* @param boolean $isPublic
* @return Quiz
*/
public function setIsPublic($isPublic)
{
$this->isPublic = $isPublic;
return $this;
}
/**
* Get isPublic
*
* @return boolean
*/
public function getIsPublic()
{
return $this->isPublic;
}
/**
* Set isActive
*
* @param boolean $isActive
* @return Quiz
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Constructor
*/
public function __construct()
{
$this->question = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add question
*
* @param \Inky\QuizBundle\Quiz $question
* @return Quiz
*/
public function addQuestion(\Inky\QuizBundle\Quiz $question)
{
$this->question[] = $question;
return $this;
}
/**
* Remove question
*
* @param \Inky\QuizBundle\Quiz $question
*/
public function removeQuestion(\Inky\QuizBundle\Entity\Quiz $question)
{
$this->question->removeElement($question);
}
/**
* Get question
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getQuestion()
{
return $this->question;
}
/**
* Set lesson
*
* @param \Inky\CourseBundle\Entity\Lesson $lesson
* @return Quiz
*/
public function setLesson(\Inky\CourseBundle\Entity\Lesson $lesson = null)
{
$this->lesson = $lesson;
return $this;
}
/**
* Get lesson
*
* @return \Inky\CourseBundle\Entity\Lesson
*/
public function getLesson()
{
return $this->lesson;
}
/**
* Set description
*
* @param string $description
* @return Quiz
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
问题类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('qContent')
->add('qType')
->add('answer', 'collection', array('type' => new AnswerType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__char_prot__'))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => null
));
}
答案类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('aContent')
->add('isRight')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => null
));
}
我希望我已经清楚了,并且所有这些代码不会让您感到困惑,如果使用一些 javascript 执行该视图。我重新发布错误以防您错过它,或者只是不想向上滚动:ErrorHandler ->handle ('2', 'spl_object_hash() 期望参数 1 是对象,给定数组', 'C:\ wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php', '1367', array( 'entity' => array('qContent' => 'Test QuestionContent', 'qType' = > '2', 'answer' => array(array('aContent' => 'Test AswerContent', 'isRight' => '1'))), 'assume' => '2'))