symfonymappingbundlevichuploaderbundle

Vichupload no mapping....?


I am trying to use the vichuploaderbundle. By following exactly the following procedure I encounter an error:

https://github.com/dustin10/VichUploaderBundle/blob/master/docs/usage.md

The error is

An exception occurred while executing 'INSERT INTO avatar (image_name, image_size, updated_at) VALUES (?, ?, ?)' with params [null, null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image_name' cannot be null

I am a little lost

php bin/console vich:mapping:debug-class App\Entity\Avatar ** that seems to be OK **

Introspecting class App\Entity\Avatar:
Found field "imageFile", storing file name in "imageName" and using mapping "avatar_images"

php bin/console vich:mapping:debug avatar_images ** that seems to be OK **

Debug information for mapping avatar_images
uri_prefix: '/images/'
upload_destination: 'D:\\Laragon\\www\\Appli_Sympho2/public/images/'
namer: array (
  'service' => NULL,
  'options' => NULL,
)
directory_namer: array (
  'service' => NULL,
  'options' => NULL,
)
delete_on_remove: true
delete_on_update: true
inject_on_load: false
db_driver: 'orm'

php bin/console vich:mapping:list-classes ** 0 classes is this normal? **

Looking for uploadable classes.
Found 0 classes.
NOTE: Only classes configured using XML or YAML are displayed.
# config/packages/vich_uploader.yaml

vich_uploader:
    db_driver: orm
    mappings:
        avatar_images:
            uri_prefix: '%app.path.avatar_images%'
            upload_destination: '%kernel.project_dir%/public%app.path.avatar_images%'
#config/services.yaml

parameters:
   app.path.avatar_images: /images  

The entity avatar

<?php

namespace App\Entity;

use App\Repository\AvatarRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
* @ORM\Entity(repositoryClass=AvatarRepository::class)
* @Vich\Uploadable
*/
class Avatar
{
   /**
    * @ORM\Id
    * @ORM\GeneratedValue
    * @ORM\Column(type="integer")
    */
   private $id;

/**
    * 
    * @Vich\UploadableField(mapping="avatar_images", fileNameProperty="imageName", size="imageSize")
    * 
    * @var File|null
    */
   private $imageFile;

   /**
    * @ORM\Column(type="string")
    *
    * @var string|null
    */
   private $imageName;

   /**
    * @ORM\Column(type="integer")
    *
    * @var int|null
    */
   private $imageSize;

   /**
    * @ORM\Column(type="datetime")
    *
    * @var \DateTimeInterface|null
    */
   private $updatedAt;

  

   public function getId(): ?int
   {
       return $this->id;
   }

/**
    *
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
    */

   public function setImageFile(?File $imageFile = null): void
   {
       $this->imageFile = $imageFile;

       if (null !== $imageFile) {
           $this->updatedAt = new \DateTimeImmutable();
       }
   }

   public function getImageFile(): ?File
   {
       return $this->imageFile;
   }

   public function setImageName(?string $imageName): void
   {
       $this->imageName = $imageName;
   }

   public function getImageName(): ?string
   {
       return $this->imageName;
   }
   
   public function setImageSize(?int $imageSize): void
   {
       $this->imageSize = $imageSize;
   }

   public function getImageSize(): ?int
   {
       return $this->imageSize;
   }
}

AvatarType.php

<?php

namespace App\Form;

use App\Entity\Avatar;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Symfony\Component\OptionsResolver\OptionsResolver;


class AvatarType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('imageFile',VichImageType::class)
            
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Avatar::class,
        ]);
    }
}

AvatarController

<?php

namespace App\Controller;

use App\Entity\Avatar;
use App\Form\AvatarType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class AvatarController extends AbstractController
{
    /**
     * @Route("/avatar", name="avatar")
     */
    public function index(Request $request, EntityManagerInterface $manager): Response
    {

         $avatar=new Avatar();
        


        $form = $this->createForm(AvatarType::class);
        $form->handleRequest($request);

     

        if ($form->isSubmitted() and $form->isValid()) {
            $manager->persist($avatar);
            $manager->flush();

            $this->addFlash(
                'success',
                'Modification enregistrée'
            );

            return $this->redirectToRoute('profile');
        }
        return $this->render('avatar/index.html.twig', [
            'form' => $form->createView()
        ]);
    }
}

Solution

  • Sorry, I find my error

    In my AvatarController, I forgot $avatar param !!!

    $form = $this->createForm(AvatarType::class,$avatar);