Jelle Mattez
Jelle Mattez

Reputation: 31

Error: Entities passed to the choice field must be managed in a multistep form

I have 2 Entities: Campaignstreams & Materialstreams. They are linked to eachother in a ManyToMany relationship.

Campaignstream

class campaignstream
{
 /**
  * @var integer
  *
  * @ORM\Column(name="id", type="integer")
  * @ORM\Id
  * @ORM\GeneratedValue(strategy="AUTO")
  */
  private $id;

 /**
  * @var string
  *
  * @ORM\Column(name="name", type="string", length=80)
  */
  private $name;

 /**
  * @ORM\ManyToMany(targetEntity="materialstream", inversedBy="campaignstreams")
  * @ORM\JoinTable(name="campaignstream_materialstream")
  *
  */
  private $materialstreams;
}

Materialstream

class materialstream
{
 /**
  * @var integer
  *
  * @ORM\Column(name="id", type="integer")
  * @ORM\Id
  * @ORM\GeneratedValue(strategy="AUTO")
  */
  private $id;

 /**
  * @var string
  *
  * @ORM\Column(name="description", type="string", length=80)
  */
  private $description;

 /**
  * @ORM\ManyToMany(targetEntity="campaignstream", mappedBy="materialstreams")
  *
  */
  private $campaignstreams;

}

Now I want to create a multi-step form to create a campaignstream with multiple materialstreams attached to it.

In my controller I've got something like this:

public function addAction(Request $request, $step)
{
  $session = $request->getSession();
  $em = $this->getDoctrine()->getManager();

  if ($step == 1) {
    $campaignstream = ($session->get('campaignstreamdata') == '') ? new campaignstream() : $session->get('campaignstreamdata');
    $CSF = $this->createForm(new CampaignstreamForm(), $campaignstream);
    $CSF->handleRequest($request);
    if ($CSF->isSubmitted() && $CSF->isValid()) {
      $session->set('campaignstreamdata', $CSF->getData());
      return $this->redirectToRoute('campaignstream_add', array('step' => 2));
    } else {
      return $this->render('CampaignBundle:Stream:add.html.php', array(
        'current' => $id, 
        'data' => $CSF->createView()
      ));
    }
  } elseif ($step == 2) {
    $campaignstream = $session->get('campaignstreamdata');
    $CSMSF = $this->createForm(new CampaignstreamMaterialstreamForm(), $campaignstream);
    if ($CSMSF->isSubmitted()) {
      $session->set('campaignstreamdata', $CSMSF->getData());
      return $this->redirectToRoute('campaignstream_add', array('step' => 3));
    } else {
      return $this->render('CampaignBundle:Stream:add.html.php', array(
        'progress' => $progress,
        'current' => $id,
        'data' => $CSMSF->createView(),
        'title' => $title,
        'campaignstream' => $campaignstream
      ));
    }
  } elseif ($step == 3) {
     ...
  }
}

Now, in the form that I use for step 2, I have this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
  $builder
    ->add('materialstreams', 'entity', array(
      'class' => 'CampaignBundle:materialstream',
      'property' => 'description',
      'multiple' => true,
      'expanded' => false,
    ));
}

Now the problem is:
I go through step 1 and fill in the name. I go through step 2 and I select the correct materialstreams. I come to step 3 and I realise I forgot 1 materialstream. I click on the link to go back to step 2, to get the error: Entities passed to the choice field must be managed

I've googled it, and tried various solutions like:

$campaignstream = $em->merge($campaignstream);

The error goes away, but my form is not filled with the previously added data, but the data does exist in the session.

I could really use some help.

Upvotes: 0

Views: 38

Answers (1)

Jelle Mattez
Jelle Mattez

Reputation: 31

Wow, after trying a lot of different things, I finally fixed it!

I had to do the merge on every materialstream separately:

} elseif ($id == 2){
  $campaignstream = $session->get('campaignstreamdata');

  $materials = $campaignstream->getMaterialstreams();

  foreach($materials as $material)
  {
    $campaignstream->removeMaterialstream($material);
    $campaignstream->addMaterialstream($em->merge($material));
  }
  ...
}

Upvotes: 1

Related Questions