VichUploaderBundle : image_name 不能为空错误
VichUploaderBundle : image_name can't be null error
我正在使用 Symfony3.1,并且我刚刚安装了 VichUploaderBundle。
我已按照文档进行操作,但出现此错误意味着 Field 'image_name' 不能为空 (null)
SQLSTATE[23000]:违反完整性约束:1048 Le champ 'image_name' ne peut être vide (null)
配置
vich_uploader:
db_driver: orm
mappings:
product_image:
uri_prefix: /images/actualites
upload_destination: kernel.root_dir/../web/images/actualites
inject_on_load: false
delete_on_update: true
delete_on_remove: true
实体
/**
*
* @Vich\UploadableField(mapping="actualite_image", fileNameProperty="imageName")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*
* @return Actualite
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
return $this;
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $imageName
*
* @return Actualite
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
表单类型:
->add('imageFile', FileType::class)
树枝
<form class="form-horizontal" method="post" enctype="multipart/form-data">
//...
{{ form_widget(form.imageFile) }}
动作
public function creerAction(Request $request)
{
$entity = new Actualite();
$form = $this->createForm(BaseType::class, $entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectToRoute('backend_actualite_liste');
}
return $this->render('Backend/Actualite/creer.html.twig',array(
'form' => $form->createView(),
)
);
}
请注意,有不同的 Symfony3 setup here,这可能会有帮助。
我认为您的设置与 Symfony3 不同。
试试这个:
->add('imageFile', VichImageType::class, array(
'required' => false,
))
另外,你也需要这样的改变:
# app/config/config.yml
twig:
form_themes:
# other form themes
- 'VichUploaderBundle:Form:fields.html.twig'
已解决
错误是config中的上传映射名称和实体中的映射名称不同,但必须相同。
在配置文件中是product_image,在实体中是actualite_image.
现在我给它们取了相同的名字,现在效果很好。
所以,实际上根据豪斯所说,这是 app/config/config.yml
文件所需的更改:
vich_uploader:
db_driver: orm
mappings:
actualite_image:
uri_prefix: /images/actualites
upload_destination: kernel.root_dir/../web/images/actualites
房屋注意事项:您不需要 config.yml 文件中的最后 3 个选项,因为它们是默认值
我正在使用 Symfony3.1,并且我刚刚安装了 VichUploaderBundle。 我已按照文档进行操作,但出现此错误意味着 Field 'image_name' 不能为空 (null)
SQLSTATE[23000]:违反完整性约束:1048 Le champ 'image_name' ne peut être vide (null)
配置
vich_uploader:
db_driver: orm
mappings:
product_image:
uri_prefix: /images/actualites
upload_destination: kernel.root_dir/../web/images/actualites
inject_on_load: false
delete_on_update: true
delete_on_remove: true
实体
/**
*
* @Vich\UploadableField(mapping="actualite_image", fileNameProperty="imageName")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*
* @return Actualite
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
return $this;
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $imageName
*
* @return Actualite
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
表单类型:
->add('imageFile', FileType::class)
树枝
<form class="form-horizontal" method="post" enctype="multipart/form-data">
//...
{{ form_widget(form.imageFile) }}
动作
public function creerAction(Request $request)
{
$entity = new Actualite();
$form = $this->createForm(BaseType::class, $entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectToRoute('backend_actualite_liste');
}
return $this->render('Backend/Actualite/creer.html.twig',array(
'form' => $form->createView(),
)
);
}
请注意,有不同的 Symfony3 setup here,这可能会有帮助。
我认为您的设置与 Symfony3 不同。 试试这个:
->add('imageFile', VichImageType::class, array(
'required' => false,
))
另外,你也需要这样的改变:
# app/config/config.yml
twig:
form_themes:
# other form themes
- 'VichUploaderBundle:Form:fields.html.twig'
已解决
错误是config中的上传映射名称和实体中的映射名称不同,但必须相同。
在配置文件中是product_image,在实体中是actualite_image.
现在我给它们取了相同的名字,现在效果很好。
所以,实际上根据豪斯所说,这是 app/config/config.yml
文件所需的更改:
vich_uploader:
db_driver: orm
mappings:
actualite_image:
uri_prefix: /images/actualites
upload_destination: kernel.root_dir/../web/images/actualites
房屋注意事项:您不需要 config.yml 文件中的最后 3 个选项,因为它们是默认值