2016-06-20 46 views
0

我正在使用Symfony3.1,并且我刚刚安装了VichUploaderBundle。 我都跟着文档,但我得到这个错误意味着字段“IMAGE_NAME”不能为空(NULL)VichUploaderBundle:image_name不能为空错误

SQLSTATE [23000]:完整性约束违规:1048乐冠军“IMAGE_NAME或者” ne peut理由韦迪(空)

配置

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; 
} 

FormType:

->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(), 
     ) 
    ); 
} 
+0

产生此错误的代码在哪里?您上面显示了实体,但我们可能需要查看控制器代码或表单代码,以便您上传。你能编辑你的文章并发布该代码吗? –

+0

@AlvinBunk我编辑了我的帖子 – hous

回答

1

所以,实际上根据什么HOUS说,这是需要对app/config/config.yml文件的变化:

vich_uploader: 
    db_driver: orm 
    mappings: 
     actualite_image: 
      uri_prefix:   /images/actualites 
      upload_destination: kernel.root_dir/../web/images/actualites 

注到HOUS:你并不需要在config.yml文件的最后3个选项,因为它们是默认的

0

正如一个音符,有不同的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' 
+0

错误在于配置中的上传映射名称和实体中的映射名称不同,但它们必须相同。 在配置文件中是product_image,在实体中是actualite_image。 现在我给他们一个相同的名字,现在效果很好。 – hous

1

解决

的错误是,在配置上载映射名称和映射名称实体是不同的,但它们必须是相同的。

在配置文件是product_image,并在实体actualite_image

现在我给他们同样的名字,它现在很好用。