【问题标题】:VichUploaderBundle not working when try to update entity尝试更新实体时,VichUploaderBundle 不起作用
【发布时间】:2018-09-11 21:19:15
【问题描述】:

我尝试通过更改图像来更新实体,但照片消失了。 db改正了,甚至调用了setImageFile方法,但是文件本身没有出现。

我的实体

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

/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
* @Vich\Uploadable
*/
class Category
{

    /**
     * @Vich\UploadableField(mapping="categories", fileNameProperty="image")
     */
    private $imageFile;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $image;

    /**
     * @ORM\Column(type="datetime")
     * @ORM\Version
     */
    private $updationDate;

    public function getImage(): ?string
    {
        return $this->image;
    }

    public function setImage(string $image): self
    {
        $this->image = $image;

        return $this;
    }

    public function getUpdationDate(): ?\DateTimeInterface
    {
        return $this->updationDate;
    }

    public function setUpdationDate(\DateTimeInterface $updationDate): self
    {
        $this->updationDate = $updationDate;

        return $this;
    }


    public function setImageFile($image): void
    {
        $this->imageFile = $image;

        if ($image instanceof UploadedFile) {
            $this->setUpdationDate(new DateTime());
        }
    }

    public function getImageFile(): ?File
    {
        return $this->imageFile;
    }
}

我的配置

vich_uploader:
db_driver: orm

mappings:
    categories:
        uri_prefix: /images/categories
        upload_destination: '%kernel.project_dir%/public/images/categories'
        namer: Vich\UploaderBundle\Naming\UniqidNamer

        inject_on_load: true
        delete_on_update: true
        delete_on_remove: true

我在以前的 stackoverflow 帖子中搜索信息,但所有解决方案都不适用于我。如果有帮助,我正在使用 Symfony 4.1

【问题讨论】:

    标签: symfony file-upload symfony-forms symfony4 vichuploaderbundle


    【解决方案1】:

    你的文件是在%kernel.project_dir%/public/images/categories上传的吗?

    您是否尝试将 delete_on_update 设置为 false?

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多