Closed
Description
I have two entities in my Bundle, Poll
and Answer
.
When creating a poll you can assign multiple answers to it.
Poll has a one-to-many relationship with Answer and this is inverted by Answer.
So Poll object has a protected property Poll::$answers
with is an ArrayCollection and the Answer object has a protected property Answer::$poll
which is a Poll
object.
With a single form i want to be able to save the poll and the answers all in one go.
Programmatically saving a new poll with two answers works perfect, all three instances are persisted in the database:
$poll = new Poll();
$poll->setBody('What is your gender?');
$poll->addAnswer( new Answer("Male") );
$poll->addAnswer( new Answer("Female") );
$em = $this->getDoctrine()->getEntityManager();
$em->persist($poll);
$em->flush();
But when i create a new Poll form with two answers by default and the user fills in all form fields i get an error message saying This form should not contain extra fields:
$entity = new Poll();
$entity->addAnswer( new Answer() );
$entity->addAnswer( new Answer() );
$form = $this->createForm(new PollType(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView()
);