Symfony 6 将对象持久化到数据库

Symfony 6 Persisting Objects to the Database

这里是显示时的错误信息:“执行查询时发生异常:SQLSTATE[23000]:违反完整性约束:1048列[=2​​5=]不能为空”

我不明白为什么在一切看起来都很正常的情况下会出现这个错误。

这是我的 DefaultController.php 文件:

<?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()]
    );
       
    }
}

这是我的实体的代码class待办事项:

<?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;
    }
}

这是我的 TodoType 表单:

<?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,
        ]);
    }
}

不幸的是,我尝试了另一种方法,即在我的控制器中注入 EntityManagerInterface 服务。我收到相同的错误消息。需要帮助。

我认为你可以在

下使用 ORM 初始化你的 属性
#[ORM\Column(type: 'boolean')]
    private $done = false;

或者您可以在提交表单后立即执行此操作,无论如何,当您提交表单时,它会将 done 值设置为 false。但是这种方法并不实用。

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();