Symfony6-可时间戳的学说扩展:特征有效,但属性无效

问题描述 投票:0回答:2

我尝试使用 StofDoctrineExtension 创建可加时间戳的实体。不幸的是,我发现使用特质效果很好,但不使用属性。

有人可以告诉我我做错了什么吗?

这有效:

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

这不起作用(当我保留实体时,时间戳会被忽略):

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

attributes traits symfony6 stofdoctrineextensions
2个回答
0
投票

也许创建/更新的私有属性没有 getters/setters?

此外,在第一个示例中,假设数据库表列为“created_at/updated_at”,而在第二个示例中为“created/updated”。

下一个代码怎么样:

#[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

0
投票

我也有同样的问题。 stof timestampable 特征忽略这些字段。当我添加实体时,属性为空。

© www.soinside.com 2019 - 2024. All rights reserved.