VichUploader:在实体更新

时间:2018-02-12 16:18:37

标签: php symfony vichuploaderbundle

我有实体Asset,其中包含字段图片

 /**
  * @ORM\Table(name="asset")
  * @ORM\Entity(repositoryClass="AppBundle\Repository\AssetRepository")
  * @Vich\Uploadable
  */
 class Asset
 {

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

     /**
      * @ORM\ManyToOne(targetEntity="Category", inversedBy="assets")
      * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
      */
     private $category;

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

     /**
      * @Vich\UploadableField(mapping="assets", fileNameProperty="image")
      * @var File
      */
     protected $imageFile;

我正在使用VichUploader将图像上传到S3存储桶。我创建了自定义名称来创建文件名。上传期间,实体上传到具有类别名称的文件夹,并以实体名称

命名
public function name($obj, PropertyMapping $mapping)
{
    $category = $obj->getCategory()->getName();
    $name = $obj->getName();
    $file = $mapping->getFile($obj);

    if ($extension = $this->getExtension($file)) {
        $name = sprintf('%s.%s', $name, $extension);
    }

    return $category.'/'.$name;
}

这些我的上传配置

oneup_flysystem:
adapters:
    assets_adapter:
        awss3v3:
            client: app.assets.s3
            bucket: '%assets_bucket%'
            prefix: assets

filesystems:
    assets_fs:
        adapter:    assets_adapter
        mount:      assets_fs

vich_uploader:
     db_driver: orm
     storage:   flysystem

mappings:
    assets:
        delete_on_remove: true
        delete_on_update: true
        uri_prefix:       'https://s3-%assets_region%.amazonaws.com/%assets_bucket%/'
        upload_destination: assets_fs
        namer: app.asset_namer

我有以下情况:用户更改资产类别。如何将文件重新上传到新的类别文件夹并更新名称?

更新

我正在使用EasyAdminBundle。哪个处理创建和编辑实体。所以我没有为Asset实体创建FormType和Controller。以下是配置:

easy_admin:
  entities:
   Asset:
    class: AppBundle\Entity\Asset
    label: 'Assets'
    form:
      fields:
        - name
        - {property: 'category', type: entity, type_options: {expanded: false, multiple: false, class: 'AppBundle:Category', required: true}}
        - {property: 'imageFile', type: 'vich_image' }

1 个答案:

答案 0 :(得分:0)

一种解决方案是更新Asset::setCategory方法逻辑,在类别名称更改时创建新的File对象,并将其传递给Asset::setImageFile以使VichUploader运行更新过程。 / p>

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @ORM\Table(name="asset")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\AssetRepository")
 * @Vich\Uploadable
 */
class Asset
{
     //...

     public function setCategory(Category $category)  
     {
         if ($this->imageFile && $this->category->getName() !== $category->getName()) {
             $this->setImageFile(new UploadedFile(
                 $this->imageFile->getRealPath(),  //physical path to image file
                 $this->image, //current image name
                 null, 
                 null, 
                 null, 
                 true
             ));
         }
         $this->category = $category;

         return $this;
     }

}

一个问题是默认情况下VichUploader不会填充实体imageFile属性。

要确保Asset::$imageFile可用,而无需与vich_uploader.storage服务互动,您需要将inject_on_load: true添加到vich_uploader.mappings设置中。这将向实体添加一个侦听器,以使用File对象自动填充imageFile属性。

#app/config/config.yml

#...

vich_uploader:
    db_driver: orm
    storage: flysystem
    mappings:
        assets:
            delete_on_remove: true
            delete_on_update: true
            uri_prefix: 'https://s3-%assets_region%.amazonaws.com/%assets_bucket%/'
            upload_destination: assets_fs
            namer: app.asset_namer
            inject_on_load: true

首选方法是将逻辑添加到controller edit action,或将custom event subscriber添加到监视资产实体的Symfony事件中。虽然我不知道如何在EasyAdminBundle中更改类别名称来实现此目的。