我尝试仅使用 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' }