VichUploaderBundle 在 delete/update 产品上引发错误
VichUploaderBundle raise error on delete/update product
我使用 symfony 4 和 VichUploaderBundle 来处理我的网络项目的图像。我根据文档做了所有事情
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,图像将被存储(按方面)并从数据库中删除产品。那么我应该怎么做才能删除图像和产品实例
在我看来,应用程序试图将 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
我使用 symfony 4 和 VichUploaderBundle 来处理我的网络项目的图像。我根据文档做了所有事情
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,图像将被存储(按方面)并从数据库中删除产品。那么我应该怎么做才能删除图像和产品实例
在我看来,应用程序试图将 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