3

我无法使用 VichUploaderBundle 删除或编辑我上传的图像。我有一个带有 OneToMany(双向关系)的 Annonce 和 Photo 实体。我尝试使用属性 setUpdatedAt 来调用 vich prePersist 但他不起作用。

这是安诺斯:

class Annonce
{
// ...
/**
 * @ORM\OneToMany(targetEntity="Immo\AnnonceBundle\Entity\Photo", mappedBy="annonce", cascade={"persist", "remove"})
 */
private $photos;

带有 setter/getterImage() 的照片实体:

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Photo
 * @Vich\Uploadable
 * @ORM\Table()
 */
class Photo
{   // id, etc. 
    /**
     * @Assert\File(
     *     maxSize="1M",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="uploads_image", fileNameProperty="url")
     *
     * @var File $image
     */
    protected $image;

    /**
     * @ORM\Column(type="string", length=255, name="url")
     *
     * @var string $url
     */
    protected $url;

    /**
     * @ORM\ManyToOne(targetEntity="Immo\AnnonceBundle\Entity\Annonce", inversedBy="photos")
     */
    private $annonce;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     *
     * @var \DateTime $updatedAt
     */
    protected $updatedAt;

/**
 * Set image
 *
 * @param string $image
 * @return Photo
 */
public function setImage($image)
{
    $this->image = $image;

    if ($this->image instanceof UploadedFile) {
        $this->updatedAt = new \DateTime('now');
    }

    return $this;
}

这是我的 config.yml:

knp_gaufrette:
stream_wrapper: ~
adapters:
    uploads_adapter:
        local:
            directory: %kernel.root_dir%/../web/img/uploads
filesystems:
    uploads_image_fs:
        adapter:    uploads_adapter

vich_uploader:
    db_driver: orm
    twig: true
    gaufrette: true
    storage:   vich_uploader.storage.gaufrette
    mappings:
        uploads_image:
            delete_on_remove: true
            delete_on_update: true
            inject_on_load: true
            uri_prefix:         img/uploads
            upload_destination: uploads_image_fs
            namer: vich_uploader.namer_uniqid

我的 Annonce 类型:

$builder->add('photos', 'collection', array('type' => new PhotoType(),
                                                'allow_add' => true,
                                                'allow_delete' => true,
                                                'by_reference' => false,
                                                )
                  )

照片类型:

$builder->add('image', 'file')

控制器:

public function updateAnnonceAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $annonce = $em->getRepository('ImmoAnnonceBundle:Annonce')
                      ->findCompleteAnnonceById($id);

        $form = $this->createForm(new AnnonceType, $annonce);

        $request = $this->get('request');

        if ($request->getMethod() == 'POST') {
            $form->bind($request);

            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();

                $em->persist($annonce);
                $em->flush();

                $this->get('session')->getFlashBag()->add('success', 'ok');

                return $this->redirect($this->generateUrl('immo_admin_annonce_homepage'));

            }
        }

        return $this->render('ImmoAnnonceBundle:Admin/Annonce:update.html.twig', array('annonce' => $annonce,
                                                                       'form' => $form->createView()
                                                                                      )
                            );
    }

我的模板在 html 中为 Annonce 中的每张照片输入了输入文件:

{{ form_widget(form.photos) }} // With JS to manage add/delete on each input.
// Return this :
<input type="file" required="required" name="immo_annoncebundle_annonce[photos][2][image]" id="immo_annoncebundle_annonce_photos_2_image">
4

2 回答 2

6

在您的实体中添加“updateAt”属性有关更多信息,请参见http://mossco.co.uk/symfony-2/vichuploaderbundle-how-to-fix-cannot-overwrite-update-uploaded-file/

于 2014-05-13T09:02:21.513 回答
1

我知道这是一个旧线程,但大卫的答案在这种情况下不起作用,因为一旦更新,OP 会尝试删除 Annonce 对象中的 Photo 对象。

我有一个类似的情况,我只需要在请求处理之后检查照片对象的路径(文件名)是否为空(在从公式执行删除操作之后),然后如果是这种情况,我手动删除照片对象调用 entitymanager 来执行操作。

查看 VichUploaderBundle 的代码(参见 UploadHandler 类的 remove 方法)显示请求删除后正在调度事件,您也可以在某处绑定到该事件 Events::POST_REMOVE (vich_uploader.post_remove) 来处理删除。尽管第一个解决方案也运行良好,但此解决方案听起来更干净。

于 2016-02-04T08:37:20.067 回答