我使用symfony 4和VichUploaderBundle处理我的Web项目的图像。我根据文档做了一切
vich_uploader:
db_driver: orm
mappings:
article_image:
uri_prefix: /image/articles
upload_destination: '%kernel.project_dir%/public/image/articles'
inject_on_load: false
delete_on_update: true
delete_on_remove: true
class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('articleTitle')
->add('articleBody')
->add('thumbNumber')
->add('seoDescription')
->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'download_label' => '...',
'download_uri' => true,
'image_uri' => true,
])
->add('save', SubmitType::class, array('label' => 'Создать статью'))
// ->add('imageSize')
// ->add('updatedAt')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Article::class,
]);
}
}
/**
* @Route("/gate/delete_article/{id}", name="delete_article")
*/
public function deleteArticle($id)
{
$article = $this->getDoctrine()
->getRepository(Article::class)
->find($id);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($article);
$entityManager->flush();
return $this->redirectToRoute("gate");
}
我有一个错误告诉我,我有问题:
Argument 1 passed to App\Entity\Article::setArticleImage() must be of the type string, null given, called in
因此图像已删除,但其余产品仍在数据库中。
public function setArticleImage(string $articleImage): self
{
$this->articleImage = $articleImage;
return $this;
}
如果我将delete_on_update:true和delete_on_remove:true设置为false,则图像将被存储(按方面)并且产品将从数据库中删除。那我该怎么做,以删除图像和产品实例
答案 0 :(得分:1)
在我看来,该应用程序试图将articleImage正确设置为null,但是由于类型提示,您不让它使用。如果要在setter中允许空值,请在前缀类型提示中添加?
(PHP 7.1 +):
public function setArticleImage(?string $articleImage): self
{
$this->articleImage = $articleImage;
return $this;
}
http://php.net/manual/en/migration71.new-features.php#migration71.new-features.nullable-types