我正在尝试在 symfony EasyAdminBundle中添加一些自定义操作,我只是添加了一个没有问题的表单视图,但问题在于这个表单 POST 操作,它是同一控制器的另一种方法,效果很好,但是当我这样做persist()
时flush()
对于实体管理器,它什么也不做。这是我的代码,除了没有在数据库中应用更改之外,一切都按预期工作。
/**
* @Route("/product-segment/save/{id}", name="admin_product_segment_save")
*/
public function saveSegmentsAction($id)
{
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('AppBundle:product')->find($id);
foreach ($product->getSegments() as $segment)
{
error_log("Product has segment " . $segment->getId());
}
$product->removeAllSegments();
foreach ($product->getSegments() as $segment)
{
error_log("Now prod has " . $segment->getId());
}
$em->persist($product);
$em->flush();
foreach ($this->getRequest()->request->all() as $post_var => $segment_id)
{
if ($post_var == 'segment_id')
{
$segment = $em->getRepository('AppBundle:Segment')->find($segment_id);
$product->addSegment($segment);
}
}
$em->persist($product);
$em->flush();
// redirect to the 'list' view of the given entity
return $this->redirectToRoute('admin', array(
'view' => 'list',
'entity' => 'Product',
));
}
编辑:这是我的实体关系
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Segment", mappedBy="products", cascade={"all"})
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(onDelete="CASCADE")}
* )
*/
protected $segments;