Symfony2: Edit file upload

后端 未结 3 1636
慢半拍i
慢半拍i 2021-02-04 18:35

I am using the cookbook article from symfony.com to implement a file upload option for images.

Now I want to load up other images to the entity.

The default stra

3条回答
  •  [愿得一人]
    2021-02-04 19:27

    If you change only the upload field the lifecycle not run the upload method, In the cookbook is reported the solution in a quote box as below:

    The PreUpdate and PostUpdate callbacks are only triggered if there is a change in one of the entity's field that are persisted. This means that, by default, if you modify only the $file property, these events will not be triggered, as the property itself is not directly persisted via Doctrine. One solution would be to use an updated field that's persisted to Doctrine, and to modify it manually when changing the file.

    add a dummy field to update in the controller before persist event as suggest by this duscussion:

    https://github.com/symfony/symfony-docs/pull/564

    public function setFile(UploadedFile $file)
    {
        $this->file = $file;
        $this->updatedAt = new \DateTime();
    }
    

提交回复
热议问题