Bonjour,
Je ne comprends pas d'où vient cette erreur alors je m'adresse à vous.
J'ai suivi la doc de Vich Uploader comme il se doit et j'ai ce message d'erreur lors de l'upload d'image :
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image_name' cannot be null
Le nom de l'image n'est pas sauvegardé, et je ne comprends pas pourquoi !
Voici mon code :
vich_uploader.yaml
vich_uploader:
db_driver: orm
mappings:
news_image:
uri_prefix: /img/uploads/news
upload_destination: '%kernel.project_dir%/public/img/uploads/news'
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
delete_on_update: true
delete_on_remove: true
News.php :
<?php
namespace App\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\NewsRepository")
* @Vich\Uploadable
*/
class News
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="news")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
/**
* @ORM\Column(type="string")
*/
private $imageName;
/**
* @Vich\UploadableField(mapping="news_image", fileNameProperty="imageName")
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*/
private $featured;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(?File $imageFile = null): self
{
$this->imageFile = $imageFile;
if ($this->imageFile instanceof UploadedFile) {
$this->updatedAt = new DateTime('now');
}
return $this;
}
public function getFeatured(): ?string
{
return $this->featured;
}
public function setFeatured(string $featured): self
{
$this->featured = $featured;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}
NewsType.php :
<?php
namespace App\Form;
use App\Entity\News;
use Symfony\Component\Form\AbstractType;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
class NewsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => 'Titre *',
])
->add('content', CKEditorType::class, [
'label' => 'Contenu *',
'attr' => ['rows' => '8'],
])
->add('imageFile', FileType::class, [
'label' => 'Ajouter une image *',
'attr' => ['lang' => 'fr'],
'required' => true
])
->add('featured', ChoiceType::class, [
'label' => 'A la Une *',
'choices' => ['Oui' => 'oui', 'Non' => 'non'],
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => News::class,
]);
}
}
AdminNewsController.php :
/**
* @Route("/admin/news/create", name="create_news")
*/
public function create(Request $request, EntityManagerInterface $em)
{
$news = new News();
$form = $this->createForm(NewsType::class, $news);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$news->setAuthor($this->getUser());
$news->setCreatedAt(new DateTime('now'));
$em->persist($news);
$em->flush();
return $this->redirectToRoute('admin_news');
}
return $this->render('admin_news/create.html.twig', [
'form' => $form->createView()
create.html.twig :
{% block body %}
{{ form_start(form) }}
{{ form_widget(form) }}
<input class="btn btn-primary text-uppercase" type="submit" value="Ajouter">
{{ form_end(form) }}
{% endblock %}
Merci pour votre aide !
Perso j'aurais mit cela
/**
* @var string|null
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $imageName;
/**
* @var File|null
* @Assert\Image()
* @Vich\UploadableField(mapping="property_image", fileNameProperty="filename")
*/
private $imageFile;