Entity with TimestampableEntity trait fails on PUT operation

177 views Asked by At

I'm on a fresh install of API Platform (v3.2.7) and I'm using Gedmo\Timestampable\Traits\TimestampableEntity

This is my entity (Greeting example)

<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

use Gedmo\Timestampable\Traits\TimestampableEntity;

#[ApiResource]
#[ORM\Entity]
class Greeting
{
    use TimestampableEntity;

    #[ORM\Id]
    #[ORM\Column(type: "integer")]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\Column]
    #[Assert\NotBlank]
    public string $name = "";

    public function getId(): ?int
    {
        return $this->id;
    }
}

and my config/services.yaml

gedmo.listener.timestampable:
    class: Gedmo\Timestampable\TimestampableListener
    tags:
        - { name: doctrine.event_listener, event: 'prePersist' }
        - { name: doctrine.event_listener, event: 'onFlush' }
        - { name: doctrine.event_listener, event: 'loadClassMetadata' }

It woks fine on POST operation, but it fails when doing a PUT. I get this error

An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null

The versions I'm using are: symfony 6.4.1, doctrine 2.12, gedmo 3.14

1

There are 1 answers

0
Matías Cánepa On BEST ANSWER

What I ended up doing was to use PATCH instead of PUT. So I can edit a partial entity and the trait still updates the updated_at field