我有以下画廊实体
class Gallery
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Tessa\GalleryBundle\Entity\Photo", mappedBy="gallery", cascade={"persist", "remove"})
*/
private $photos;
/* ... */
}
这gallery
与与实体的manyToOne
关系相关联。PointOfInterest
这是声明
class PointOfInterest
{
/* ... */
/**
* @ORM\ManyToOne(targetEntity="Tessa\GalleryBundle\Entity\Gallery", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $gallery;
/* ... */
我还使用表单来更新PointOfInterest
实体。这是表单声明
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('gallery', new GalleryType())
;
}
和GalleryType
宣言。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('photos', 'collection', array('type' => new PhotoType(),
'required' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
))
;
}
当我编辑时,PoI
我可以毫无问题地将照片添加到画廊,但我不能删除任何东西。
我试图挂上 gallery PreUpdate
,但它从未被调用。removePhotos
我以实体的方式打印输出,Gallery
照片从图库中删除。然后我怀疑画廊永远不会被坚持下去。
这是我坚持PoI
编辑后的代码。
private function handleForm($elem, $is_new)
{
$form = $this->createForm(new CircuitType, $elem);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($elem);
$em->flush();
return $this->redirect($this->generateUrl('tessa_circuit_index'));
}
}
return $this->render('TessaUserBundle:Circuits:add.'.'html'.'.twig',
array(
'form' => $form->createView(),
'is_new' => $is_new,
));
}