2013-04-24 77 views
20

刪除一個項目,我有以下畫廊實體從一對多關係

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; 

    /* ... */ 
} 

gallerymanyToOne關係到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, 
     )); 
} 
+0

顯示的錯誤是什麼? – Sybio 2013-04-24 18:16:34

+0

沒有錯誤。我只是不知道如何編輯我的代碼,使其行爲像我想要的。 – tomahh 2013-04-24 23:28:00

+0

Duplicate:http://stackoverflow.com/questions/8923919/symfony2-form-collection-how-to-remove-entity-from-a-collection – 2015-01-18 14:33:45

回答

59

有關於處理這種情況的article in Symfony2 cookbook。由於您有OneToMany關係,您必須在控制器中手動刪除相關對象。

編輯: 或者您可以利用Doctrine's orphan removal功能。

class Gallery 
{ 
    //...  

    /** 
    * @ORM\OneToMany(targetEntity="Photo", mappedBy="gallery", cascade={"persist", "remove"}, orphanRemoval=true) 
    */ 
    private $photos; 

    //... 

    public function removePhotos($photo) 
    { 
     $this->photos->remove($photo); 
     $photo->setGallery(null); 
    } 
} 
+0

這裏的問題是,畫廊不能自己工作。我在多個實體中使用它,並需要複製我的代碼。我想找一個DRY解決方案。 – tomahh 2013-04-24 23:29:11

+4

@TomAhh在這種情況下,我沒有看到任何標準的解決方案,除非你可以嘗試在'$ photos'關聯上設置'orphanRemoval = true'。然後在'removePhotos($ photo)'庫中調用'$ photo-> setGallery(null)''。 – gatisl 2013-04-25 08:00:48

+0

這是一個完美的解決方案,謝謝!它現在的作品完全像我想要的一樣。你能否更新你的答案,以便我可以驗證它? – tomahh 2013-04-25 08:31:00