2013-04-24 40 views
20

Tôi có thực thể Gallery sauXóa một mục từ mối quan hệ oneToMany

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 này được liên kết với một mối quan hệ manyToOne đến một thực thể PointOfInterest. Dưới đây là tuyên bố

class PointOfInterest 
{ 
/* ... */ 
/** 
* @ORM\ManyToOne(targetEntity="Tessa\GalleryBundle\Entity\Gallery", cascade={"persist", "remove"}) 
* @ORM\JoinColumn(nullable=false) 
*/ 
private $gallery; 
/* ... */ 

Tôi cũng sử dụng Biểu mẫu để cập nhật thực thể PointOfInterest. Dưới đây là tuyên bố biểu mẫu

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
      ->add('name',   'text') 
      ->add('gallery',  new GalleryType()) 
     ; 
} 

và khai báo 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 
              )) 
    ; 
} 

Khi tôi chỉnh sửa PoI tôi có thể thêm ảnh vào thư viện mà không vấn đề, nhưng tôi không thể xóa bất cứ điều gì.

Tôi đã cố gắng để treo trên bộ sưu tập PreUpdate, nhưng nó không bao giờ được gọi. Tôi in đầu ra theo phương thức removePhotos của thực thể Gallery và ảnh sẽ bị xóa khỏi thư viện. Sau đó tôi nghi ngờ Gallery không bao giờ được tiếp tục tồn tại.

Đây là mã khi tôi duy trì PoI sau khi chỉnh sửa.

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

Lỗi hiển thị là gì? – Sybio

+0

Không có lỗi. Tôi chỉ không biết làm thế nào để chỉnh sửa mã của tôi để làm cho hành vi này như tôi muốn. – tomahh

+0

Nhân bản: http://stackoverflow.com/questions/8923919/symfony2-form-collection-how-to-remove-entity-from-a-collection –

Trả lời

59

article in Symfony2 cookbook về việc xử lý loại tình huống này. Khi bạn có mối quan hệ OneToMany, bạn phải loại bỏ các đối tượng liên quan theo cách thủ công trong bộ điều khiển.

Chỉnh sửa: Hoặc bạn có thể sử dụng tính năng 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

Vấn đề ở đây là Thư viện không thể tự làm việc được. Tôi sử dụng nó trong thực thể máy chủ, và sẽ cần phải lặp lại mã của tôi hơn. Tôi muốn tìm một giải pháp DRY. – tomahh

+4

@TomAhh Trong trường hợp này tôi không thấy bất kỳ giải pháp tiêu chuẩn nào, ngoại trừ có lẽ bạn có thể thử đặt 'orphanRemoval = true' trên liên kết' $ photos'. Và sau đó gọi phương thức '$ photo-> setGallery (null)' bên trong 'removePhotos ($ photo)' của thư viện. – gatisl

+0

Đó là một giải pháp hoàn hảo, cảm ơn! Nó bây giờ hoạt động chính xác như tôi muốn nó quá. Bạn có thể cập nhật câu trả lời của mình để tôi có thể xác thực câu trả lời không? – tomahh

Các vấn đề liên quan