嗨,我一直在网上搜索,我不明白为什么会收到此错误。我在这段代码中看不到任何错误。 1票可以有很多答案,1个答案可以有一票
错误是:
找到Doctrine \ Common \ Collections \ ArrayCollection类型的实体 协会myvote \ usuariosBundle \ Entity \ Answer#vote,但期待 myvote \ usuariosBundle \实体\投票
我的课程Answer.php
命名空间myvote \ usuariosBundle \ Entity;
将Doctrine \ ORM \ Mapping用作ORM;使用Doctrine \ Common \ Collections \ ArrayCollection;
/**
* Answer
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="myvote\usuariosBundle\Entity\AnswerRepository")
*/
class Answer {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="id_vote", type="integer")
*/
private $idVote;
/**
* @var string
*
* @ORM\Column(name="answer", type="string", length=255)
*/
private $answer;
/**
* @var integer
*
* @ORM\Column(name="count", type="integer")
*/
private $count;
/**
* @ORM\ManyToOne(targetEntity="Vote", inversedBy="answers")
* @ORM\JoinColumn(name="id_vote", referencedColumnName="id")
*/
protected $vote;
...
我的班级Vote.php
<?php
namespace myvote\usuariosBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Vote
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="myvote\usuariosBundle\Entity\VoteRepository")
*/
class Vote {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="id_user", type="integer")
*/
private $idUser;
/**
* @var string
*
* @ORM\Column(name="question", type="string", length=255)
*/
private $question;
/**
* @ORM\OneToMany(targetEntity="Answer", mappedBy="vote")
*/
protected $answers;
Controller.php这样
public function createVoteAction(Request $request)
{
$form = $this->createForm(new CreateVoteType());
$form->handleRequest($request);
if($form->isValid()) {
$postData = $request->request->get('create');
$session = $this->getRequest()->getSession();
$vote = new Vote();
$vote->setIdUser($session->get('id_user'));
$vote->setQuestion($postData['title']);
$vote->setPassword($postData['password']);
$vote->setDateStart(new \DateTime($postData['date_start']));
$vote->setDateFinish(new \DateTime($postData['date_finish']));
$vote->setTimeRest($postData['time_rest']);
$vote->setDateTime(new \DateTime("now"));
$vote->setType($postData['type_vote']);
$em = $this->getDoctrine()->getManager();
$em->persist($vote);
$em->flush();
for($i=1;$i<=$postData['total_option'];$i++){
$answer = new Answer();
$answer->setIdVote($vote);
$answer->setAnswer($postData['option'.$i]);
$answer->setCount(0);
$em->persist($answer);
$em->flush();
}
return $this->redirect($this->generateUrl('myvoteusuarios_createvotepage'));
}
return $this->render('myvoteusuariosBundle:Default:create_vote.html.twig', array(
'createVoteForm' => $form->createView(),
));
}
投票正确存储,但没有答案(出现错误) 它能是什么? 谢谢!