I want to use vichuploader to manage image upload on my Symfony 5.4 project. I got the image_name cannot be null SQL error but can't find why... The mapping naming is good the updateAt field is filled. I don't see anything. Thanks for your help.
Here is my code :
vich_uploader.yml
vich_uploader:
db_driver: orm
mappings:
images:
uri_prefix: /uploads/images
upload_destination: '%kernel.project_dir%/public/uploads/images'
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
Entity handling 3 image entity
<?php
namespace App\Entity;
use App\Repository\BabyStuffRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Trait\MetaDataTrait;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource()]
#[ORM\Entity(repositoryClass: BabyStuffRepository::class)]
#[ORM\HasLifecycleCallbacks]
class BabyStuff
{
use MetaDataTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private ?string $title = null;
#[ORM\Column(type: Types::TEXT)]
#[Assert\NotBlank]
private ?string $description = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\NotBlank]
private ?string $city = null;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\NotBlank]
#[Assert\Country]
private ?string $country = null;
#[ORM\ManyToOne(inversedBy: 'babyStuffs')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[ORM\Column(length: 50, nullable: true)]
#[Assert\NotBlank]
private ?string $state = null;
#[ORM\Column]
#[Assert\NotBlank]
#[Assert\PositiveOrZero]
private ?float $price = null;
#[ORM\ManyToOne(inversedBy: 'babystuff')]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotBlank]
private ?Category $category = null;
#[ORM\Column]
private ?bool $isOnHome = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotNull]
private ?Image $image1 = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
private ?Image $image2 = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
private ?Image $image3 = null;
private $slugger;
public function __construct(/* SluggerInterface $slugger */)
{
/* $this->slugger = $slugger; */
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function getSlugTitle(): string
{
return $this->slugger->slug($this->title);
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getState(): ?string
{
return $this->state;
}
public function setState(?string $state): self
{
$this->state = $state;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function isIsOnHome(): ?bool
{
return $this->isOnHome;
}
public function setIsOnHome(bool $isOnHome): self
{
$this->isOnHome = $isOnHome;
return $this;
}
public function getImage1(): ?Image
{
return $this->image1;
}
public function setImage1(Image $image1): self
{
$this->image1 = $image1;
return $this;
}
public function getImage2(): ?Image
{
return $this->image2;
}
public function setImage2(?Image $image2): self
{
$this->image2 = $image2;
return $this;
}
public function getImage3(): ?Image
{
return $this->image3;
}
public function setImage3(?Image $image3): self
{
$this->image3 = $image3;
return $this;
}
}
my Image entity
<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\ImageRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: ImageRepository::class)]
#[ApiResource]
#[Vich\Uploadable]
class Image
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[Vich\UploadableField(mapping: 'images', fileNameProperty: 'imageName')]
private ?File $imageFile = null;
#[ORM\Column(length: 255)]
private ?string $imageName = null;
#[ORM\Column(type: 'datetime')]
private $updatedAt;
public function getId(): ?int
{
return $this->id;
}
public function getImageFile(): ?string
{
return $this->imageFile;
}
/**
* @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 getImageName(): ?string
{
return $this->imageName;
}
public function setImageName(string $imageName): self
{
$this->imageName = $imageName;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}
My main form type
<?php
namespace App\Form;
use App\Entity\BabyStuff;
use App\Entity\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
class BabyStuffType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title', TextType::class, [
'attr' => ['class' => 'form-control', 'placeholder' => 'Titre de l\'annonce'],
])
->add('description', TextareaType::class, [
'attr' => ['class' => 'form-control no-height', 'placeholder' => 'Description', 'rows' => 5],
])
->add('category', EntityType::class, [
'class' => Category::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.isOnline = true')
->orderBy('c.title', 'ASC');
},
'choice_label' => 'title',
'attr' => ['class' => 'form-control', 'placeholder' => 'Cat?�gorie'],
])
->add('state', ChoiceType::class, [
'choices' => [
'Neuf' => 'NEW',
'Bon ?�tat' => 'GOOD',
'Etat satisfaisant' => 'USED',
],
'attr' => ['class' => 'form-control', 'placeholder' => 'Etat'],
])
->add('price', NumberType::class, [
'attr' => ['class' => 'form-control', 'placeholder' => 'Prix/jour'],
])
->add('city', TextType::class, [
'attr' => ['class' => 'form-control', 'placeholder' => 'Ville'],
])
->add('country', CountryType::class, [
'attr' => ['class' => 'form-control', 'placeholder' => 'Pays'],
])
->add('image1', ImageType::class)
->add('image2', ImageType::class, [
'required' => false,
])
->add('image3', ImageType::class, [
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => BabyStuff::class,
]);
}
}
My Image form type
<?php
namespace App\Form;
use App\Entity\Image;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'delete_label' => 'Supprimer',
'download_label' => 'T?�l?�charger',
'download_uri' => true,
'image_uri' => true,
/* 'imagine_pattern' => 'product_photo_320x240', */
'asset_helper' => true,
'label' => false
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Image::class,
]);
}
}
I just found out what was the problem... I'm using Attributes instead of Annotations and for some reasons Vich don't work with Attributes on my env. Changing to Annotations made things work.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Symfony compares favorably with other PHP frameworks in terms of reliability and maturity. This framework appeared a long time ago, in 2005, that is, it has existed much longer than most of the other tools we are considering. It is popular for its web standards compliance and PHP design patterns.
https://symfony.com/
Foundation, similar to Bootstrap, has become very popular as a more complex framework with some advanced but easy-to-implement CSS components. It is built with Sass, so just like Bootstrap, it is customizable. In addition to this, it also boasts some features that help make the design mobile responsive.
https://get.foundation/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.