0

i try to make timestampable entity with StofDoctrineExtension. Unfortunatly i found that using trait works perfectly, but not using attributes.

Could someone says me what i does wrong ?

this works :

<?php

namespace App\Entity;

use App\Repository\UserPictureRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;

#[ORM\Entity(repositoryClass: UserPictureRepository::class)]
#[ORM\HasLifecycleCallbacks]
class UserPicture
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

    use TimestampableEntity;

this doesn't work (timestampable is ignorated when i persist entity):

<?php

namespace App\Entity;

use App\Repository\UserPictureRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;

#[ORM\Entity(repositoryClass: UserPictureRepository::class)]
#[ORM\HasLifecycleCallbacks]
class UserPicture
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

     #[ORM\Column(type: Types::DATETIME_MUTABLE)]
     #[Gedmo\Timestampable(on: 'create')]
     private ?\DateTimeInterface $created = null;

     #[ORM\Column(type: Types::DATETIME_MUTABLE)]
     #[Gedmo\Timestampable]
     private ?\DateTimeInterface $updated = null;

mysterty
  • 31
  • 7

1 Answers1

0

Perhaps there are no getters/setters for created/updated private properties?

Also, in the first example, the database table columns 'created_at/updated_at' are assumed, while in the second "created/updated".

What about next code:

#[HasLifecycleCallbacks]
class UserPicture
{
    #...

    #[ORM\Column]
    #[Timestampable(on: 'create')]
    private ?DateTimeImmutable $created = null;

    #[ORM\Column]
    #[Timestampable(on: 'update')]
    private ?DateTimeImmutable $updated = null;

    public function getCreated(): ?DateTimeImmutable
    {
        return $this->created;
    }

    public function setCreated(DateTimeImmutable $created): self
    {
        $this->created = $created;

        return $this;
    }

    public function getUpdated(): ?DateTimeImmutable
    {
        return $this->updated;
    }

    public function setUpdated(DateTimeImmutable $updated): self
    {
        $this->updated = $updated;

        return $this;
    }
}
#stof_doctrine_extensions.yaml
stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            timestampable: true
vodevel
  • 144
  • 4