Here is the error message when displaying: "An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'done' cannot be null"
I don't understand why this error occurs when everything seems very normal.
Here's my DefaultController.php file:
<?php
namespace App\Controller;
use App\Entity\Todo;
use App\Form\TodoType;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class DefaultController extends AbstractController {
#[Route('/', name:'index')]
public function index(Request $request, ManagerRegistry $mr) {
$todo = new Todo();
$todoForm = $this->createForm(TodoType::class, $todo);
$todoForm->handleRequest($request);
// dd($todoForm);
if($todoForm->isSubmitted() && $todoForm->isValid() ){
$todo->setCreatedAt(new \DateTime());
$entityManager = $mr->getManager();
$entityManager->persist($todo);
$entityManager->flush();
}
return $this->render('./page1.html.twig',
['form'=>$todoForm->createView()]
);
}
}
Here is the code of my entity class todo:
<?php
namespace App\Entity;
use App\Repository\TodoRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TodoRepository::class)]
class Todo
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $content;
#[ORM\Column(type: 'boolean', options:["default" => false])]
private $done;
#[ORM\Column(type: 'datetime' , options:["default" => "CURRENT_TIMESTAMP"])]
private $createdAt;
public function getId(): ?int
{
return $this->id;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getDone(): ?bool
{
return $this->done;
}
public function setDone(bool $done): self
{
$this->done = $done;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
}
Here is my TodoType form:
<?php
namespace App\Form;
use App\Entity\Todo;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TodoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('content')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Todo::class,
]);
}
}
I tried another method by injecting the EntityManagerInterface service in my controller, unfortunately. I get the same error message. Need help please.
CodePudding user response:
I think instead of initialising your property withing the ORM you can do it underneath
#[ORM\Column(type: 'boolean')]
private $done = false;
Or you could do it as soon as you submit the form, by any means when you submit the form it sets the done value to false. However this method isn't practical.
if($todoForm->isSubmitted() && $todoForm->isValid() ){
$todo->setCreatedAt(new \DateTime());
$todo->setDone(0); // Sets the value to false as soon as the form is submitted
$entityManager = $mr->getManager();
$entityManager->persist($todo);
$entityManager->flush();