刪除一個項目,我有以下畫廊實體從一對多關係
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
我可以將照片添加到庫中沒有問題,但我不能刪除任何東西。
我試圖掛在畫廊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,
));
}
顯示的錯誤是什麼? – Sybio 2013-04-24 18:16:34
沒有錯誤。我只是不知道如何編輯我的代碼,使其行爲像我想要的。 – tomahh 2013-04-24 23:28:00
Duplicate:http://stackoverflow.com/questions/8923919/symfony2-form-collection-how-to-remove-entity-from-a-collection – 2015-01-18 14:33:45