I have the following Gallery entity
class Gallery
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection
* @ORMOneToMany(targetEntity="TessaGalleryBundleEntityPhoto", mappedBy="gallery", cascade={"persist", "remove"})
*/
private $photos;
/* ... */
}
This gallery
is linked with a manyToOne
relationship to a PointOfInterest
entity. Here is the declaration
class PointOfInterest
{
/* ... */
/**
* @ORMManyToOne(targetEntity="TessaGalleryBundleEntityGallery", cascade={"persist", "remove"})
* @ORMJoinColumn(nullable=false)
*/
private $gallery;
/* ... */
I also use a Form to update the PointOfInterest
entity. Here is the form declaration
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('gallery', new GalleryType())
;
}
and the GalleryType
declaration.
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
))
;
}
When I edit the PoI
I can add photos to the gallery without problem, but I can't delete anything.
I tried to hook on gallery PreUpdate
, but it is never called. I printed output in removePhotos
method of Gallery
entity, and the photos are removed from the gallery. I then suspect the Gallery to never be persisted.
Here is the code when I persist the PoI
after editing.
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,
));
}
See Question&Answers more detail:
os