Search code examples
symfonydoctrine-ormlifecycle

HasLifecycleCallbacks dont work in symfony 6.2


#[ORM\Entity(repositoryClass: MaintenanceBateauRepository::class)]
#[ORM\HasLifecycleCallbacks]
class MaintenanceBateau
{
    #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $createdAt = null;

....
    /**
     * @ORM\PrePersist()
     */
    public function onPrePersist()
    {
        $this->createdAt = new \DateTimeImmutable();
    }

He need any config for work?

If I add an object MaintenanceBateau , the data base row is correct but the createdAt is Null! It should be "Today" Thanks for help


Solution

  • An Attribute starts with #[ and an Annotation starts with @. you cannot use both Attributes (e.g. #[ORM\HasLifecycleCallbacks]) and Annotations (e.g. /** @ORM\PrePersist() */) together, you must use one or the other and configure Doctrine to use the one you have selected. In your case, probably replace

        /**
         * @ORM\PrePersist()
         */
    

    with

        #[Orm\PrePersist()]