Reputation: 43
I have a Entity that have a ManyToOne with a File Entity.
My problem is when I try to delete.
This is how I create:
/**
* Creates a new Catalogo entity.
*
* @Route("/create", name="catalogo_create")
* @Method("POST")
* @Template("BWSBajaCupcakesBundle:Catalogo:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Catalogo();
$file = new Archivo();
$form = $this->createForm(new CatalogoType(), $entity);
$form->bind($request);
$file_form = false;
if($form['file']->getData()){
$file_form = $form['file'];
//unset($form['file']);
}
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
if($file_form){
$tipoImagen = $em->getRepository('BWSBajaCupcakesBundle:TipoArchivo')->find(1);
$file->setFile($file_form->getData());
$file->setPrincipal(true);
$file->setTipo($tipoImagen);
$file->setFechaCaptura(date_create(date("Y-m-d H:i:s")));
$file->upload();
$em->persist($file);
$entity->setImagen($file);
}
$em->flush();
return $this->redirect($this->generateUrl('catalogo_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
This is how I delete:
/**
* Deletes a Catalogo entity.
*
* @Route("/{id}/delete", name="catalogo_delete")
* @Method("POST")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('BWSBajaCupcakesBundle:Catalogo')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Catalogo entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('catalogo'));
}
This is my relationship:
/**
* @ORM\ManyToOne(targetEntity="BWS\BajaCupcakesBundle\Entity\Archivo", cascade={"all"})
* @ORM\JoinColumn(name="imagen_id", referencedColumnName="id")
*/
private $imagen;
I dont get it, I did this in my other Symfony applications and never had this issue.
Thanks in advance for your help.
Cheers
Upvotes: 1
Views: 945
Reputation: 17282
Although you have the answer in your comment, I'd tought I'd give the code in any case (based on the cookbook document entity):
/**
* Pre remove upload
*
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->temp = $this->getAbsolutePath();
}
/**
* Remove upload
*
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($this->temp) {
unlink($this->temp);
}
}
Upvotes: 5