1

我有一个带有文件实体的 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 应用程序中这样做过,但从未遇到过这个问题。

在此先感谢您的帮助。

干杯

4

1 回答 1

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);
    }
}
于 2013-08-23T13:08:09.333 回答