我已经使用SonataAdmin设置了VichUploaderBundle,我不想使用奏鸣曲来分类实体上传图像。
表单呈现正常,实体保存正常,发送多部分数据和图像在那里。但它没有保存,也没有错误或其他东西需要调试。
配置:
vich_uploader:
db_driver: orm
mappings:
category_image:
uri_prefix: /uploads/images/category
upload_destination: %kernel.root_dir%/../web/uploads/images/category
inject_on_load: true
delete_on_remove: true
delete_on_update: true
namer: vich_uploader.namer_origname
CategoryAdmin:
$formMapper
->add('title', 'text', ['label' => 'title'])
->add('description', 'textarea', ['required' => false, 'label' => 'description'])
->add('image', 'file',
[
'required' => false,
'label' => 'image',
'data_class' => 'Symfony\Component\HttpFoundation\File\File',
'attr' => ['class' => 'sonata-medium-file'],
]
);
CategoryEntity:
/**
* @ORM\Column(type="string", length=255, name="image_name", nullable=true)
*/
protected $imageName;
/**
* @Assert\File(
* groups={"list"},
* maxSize="500k",
* mimeTypes={"image/png", "image/jpeg", "image/pjpeg"},
* mimeTypesMessage = "constraint.mime_type"
* )
* @Vich\UploadableField(
* mapping="category_image",
* fileNameProperty="imageName"
* )
* @var File $image
*/
protected $image;
POST:content
Content-Disposition: form-data; name="s5519586317457[image]"; filename="ico.png"
内容类型:image / png
答案 0 :(得分:2)
VichUploaderBundle和Doctrine有两件事需要了解:
Symfony\Component\HttpFoundation\File\UploadedFile
个对象时,捆绑包才会触发上传机制,因此为您的字段定义data_class
选项会中断(您可以安全地删除它)。 N.B :您可能还想检查您的实体是否定义为@Vich\Uploadable