Hi, I've been searching on Internet and I don't understand why I get this error. I don't see any error in this code. 1 vote can have many answers and 1 answer can have one vote
The error is:
Found entity of type Doctrine\Common\Collections\ArrayCollection on association myvote\usuariosBundle\Entity\Answer#vote, but expecting myvote\usuariosBundle\Entity\Vote
My class Answer.php
namespace myvote\usuariosBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use 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;
...
My class 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(),
));
}
The vote is stored correctly, but no answers(appears the error) What can it be? Thanks!