2013-05-10 3 views
1

파일 엔티티가있는 ManyToOne이있는 엔티티가 있습니다.Symfony2 엔티티를 찾을 수 없습니다 - 파일 관계에서 업로드

내 문제는 삭제하려고하는 경우입니다.

/** 
    * 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')); 
} 

이 내 관계 :

/** 
* 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(), 
    ); 
} 

이 내가 삭제하는 방법입니다

내가 만드는 방법입니다

/** 
* @ORM\ManyToOne(targetEntity="BWS\BajaCupcakesBundle\Entity\Archivo", cascade={"all"}) 
* @ORM\JoinColumn(name="imagen_id", referencedColumnName="id") 
*/ 
private $imagen; 

나는 그것을 얻을 해달라고, 내가 이런 짓을 내 다른 Symfony 응용 프로그램에서는이 문제가 없었습니다.

미리 도움을 주셔서 감사합니다.

건배

+1

얼굴 손바닥 I 내 File 엔티티에서 PostRemove 콜백 사이클에 대한 removeUpload를 가졌습니다 ... 이름과 웹 경로를 사용하여 PostRemove 전에 엔티티를 삭제하고있었습니다. 즉, 엔티티를 제거한 alredy 엔티티에서 수행 할 수 있습니다. 그래서 기본적으로 나는 entitypath에 dindt가 종료하는 웹 경로와 파일 이름을 부르고있었습니다. 이 cand을 사용하기를 바랍니다. –

답변

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); 
    } 
}