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,
));
}
Lỗi hiển thị là gì? – Sybio
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
Nhân bản: http://stackoverflow.com/questions/8923919/symfony2-form-collection-how-to-remove-entity-from-a-collection –