我有2个实体:Campaignstreams& Materialstreams。 它们在ManyToMany关系中彼此链接。
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;
}
现在,我想创建一个多步骤表单来创建一个附加了多个物流的广告系列流。
在我的控制器里,我有这样的事情:
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) {
...
}
}
现在,在我用于第2步的表单中,我有:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('materialstreams', 'entity', array(
'class' => 'CampaignBundle:materialstream',
'property' => 'description',
'multiple' => true,
'expanded' => false,
));
}
现在的问题是:
我通过第1步填写名称。我通过第2步,选择正确的物流。我来到第3步,我意识到我忘记了1个物流。我点击该链接返回步骤2,以获取错误:必须管理传递到选择字段的实体
我用谷歌搜索了它,并尝试了各种解决方案,如:
$campaignstream = $em->merge($campaignstream);
错误消失了,但我的表单没有填充先前添加的数据,但数据确实存在于会话中。
我真的可以使用一些帮助。
答案 0 :(得分:0)
我必须分别在每个物流上进行合并:
} elseif ($id == 2){
$campaignstream = $session->get('campaignstreamdata');
$materials = $campaignstream->getMaterialstreams();
foreach($materials as $material)
{
$campaignstream->removeMaterialstream($material);
$campaignstream->addMaterialstream($em->merge($material));
}
...
}