0

I'm using Symfony3.1 and I have just installed VichUploaderBundle. I have followed the documentation but I get this error means that Field 'image_name' can not be empty (null)

SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'image_name' ne peut être vide (null)

Config

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

the entity

/**
 *
 * @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)

twig

<form class="form-horizontal" method="post" enctype="multipart/form-data">
 //...
 {{ form_widget(form.imageFile) }}

Action

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(),
        )
    );
}
hous
  • 2,577
  • 2
  • 27
  • 66
  • Where is the code that is producing this error. You show the Entity above, but we probably need to see the Controller code or form code where you do the upload. Can you edit your post and post that code please? – Alvin Bunk Jun 20 '16 at 15:32
  • @AlvinBunk I have edited my post – hous Jun 20 '16 at 15:46

3 Answers3

1

Solved

The error is that the upload mapping name in the config and the mapping name in the entity are different but they must be the same.

In the config file is product_image and in the entity is actualite_image.

Now I give them the same name and it works good now.

hous
  • 2,577
  • 2
  • 27
  • 66
1

So, actually according to what hous stated, this is the change required to the app/config/config.yml file:

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

Note to hous: You don't need those last 3 options in the config.yml file, since they are the default

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
0

Just as a note, there is different Symfony3 setup here, which might be helpful.

I think for your setup, it's different for Symfony3. Try this:

->add('imageFile', VichImageType::class, array(
    'required'      => false,
))

Also, you need this change too:

# app/config/config.yml
twig:
    form_themes:
        # other form themes
        - 'VichUploaderBundle:Form:fields.html.twig'
Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • The error is that the upload mapping name in the config and the mapping name in the entity are different but they must be the same. In the config file is product_image and in the entity is actualite_image. Now I give them the same name and it works good now. – hous Jun 20 '16 at 16:13