我有一个具有文件实体的ManyToOne的实体。
我的问题是当我尝试删除时。
这就是我创建的方式:
/**
* 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(),
);
}
这是我删除的方式:
/**
* 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'));
}
这是我的关系:
/**
* @ORM\ManyToOne(targetEntity="BWS\BajaCupcakesBundle\Entity\Archivo", cascade={"all"})
* @ORM\JoinColumn(name="imagen_id", referencedColumnName="id")
*/
private $imagen;
我不明白,我在其他Symfony应用程序中这样做了,从来没有遇到过这个问题。
提前感谢您的帮助。
干杯
答案 0 :(得分:5)
虽然你的评论中有答案,但我认为我会在任何情况下给出代码(基于食谱文档实体):
/**
* 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);
}
}