我尝试仅使用YML配置Vich并面对问题。 我创建声明文件(路径:src / My / GreatBundle / Resources / config / vich_uploader / Media.yml):
My\GreatBundle\Entity\Media:
image:
mapping: image_mapping
filename_property: file_name
创建实体媒体(路径:src / My / GreatBundle / Entity / Media.php):
<?php
namespace My\GreatBundle\Entity;
use Symfony\Component\HttpFoundation\File\File;
class Media{
private $id;
public function getId(){
return $this->id;
}
protected $file_name;
protected $image;
public function setFileName($fileName){
$this->file_name = $fileName;
return $this;
}
public function getFileName(){
return $this->file_name;
}
public function setImage(File $image){
$this->image = $image;
return $this;
}
public function getImage(){
return $this->image;
}
}
最后创建表单(路径:src / My / GreatBundle / Entity / Form / Type / MediaType.php):
<?php
namespace My\GreatBundle\Entity\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use My\GreatBundle\Entity\Media;
use Vich\UploaderBundle\Form\Type\VichImageType;
class MediaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('image', VichImageType::class, [
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver){
$resolver->setDefaults(array(
'data_class' => Media::class,
));
}
public function getName(){
return 'my_great_type_media';
}
}
想念什么是错过的?我有这样的信息:
The class "My\GreatBundle\Entity\Media" is not uploadable. If you use annotations to configure VichUploaderBundle, you probably just forgot to add `@Vich\Uploadable` on top of your entity. If you don't use annotations, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue.
显然我有清晰的缓存。
编辑1:config.yml
vich_uploader:
db_driver: orm
mappings:
image:
uri_prefix: /media
upload_destination: '%kernel.root_dir%/../uploads/media'
delete_on_remove: true
delete_on_update: false
inject_on_load: false
namer:
service: vich_uploader.namer_property
options: { property: 'id' }
答案 0 :(得分:0)
<强>更新:强>
正如我所看到的,在您的配置中,您的filename_property称为 file_name ,但您的表单使用属性 image 作为文件名属性。
此外,在您的Media.yml
文件中,您使用的是名为 image_mapping 的地图名称,但在config.yml
中,您的地图名称为图片。
我不知道你为什么要避免注释,但是当你使用它们时,生活会变得容易得多:)
此问题存在相关问题,因此请检查How to configure VichUploader in entity mapped by yml?