私は Symfony 2 が初めてで、まだすべてを理解していないと言わざるを得ません。これが私が達成しようとしていることです。「クイズ」ページで、管理者は新しいクイズを追加します。管理者は、jQuery とネストされたフォーム「質問」を使用して、必要な数だけ新しい質問を動的に追加できます。次に、第 2 レベルが作成されます。彼が作成する質問ごとに、彼は回答を作成し、正しい回答として 1 つまたは複数を選択できます。基本的に、多肢選択式の質問が残ります。
データの永続性を除いてすべてを機能させることができました 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 が配列ではなくエンティティを必要としていることは理解していますが、データ永続化プロセスを完全に再構築することなく、フォームが返すものを永続化するにはどうすればよいでしょうか?!
わかりました、あなたは十分に待っていました、これがコードです^^
AddQuiz コントローラー:
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で実行した場合のビューです。エラーを見逃した場合、または単に上にスクロールしたくない場合に備えて、エラーを再投稿します。 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'))