簡體   English   中英

找不到Symfony2實體-關系中的文件上傳

[英]Symfony2 Entity was not found - FIle Upload in a relationship

我有一個帶有文件實體的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應用程序中做到了這一點,但從未遇到過此問題。

在此先感謝您的幫助。

干杯

盡管您的評論中給出了答案,但無論如何我還是會給出代碼(基於菜譜文檔實體):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM