具有时间戳属性的实体不起作用

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

我正在全新安装 API Platform (v3.2.7),并且我正在尝试在示例 Greeting 实体上实现 Gedmo 的 Timestampable。我首先尝试使用属性

<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\DBAL\Types\Types;

/**
 * This is a dummy entity. Remove it!
 */
#[ApiResource]
#[ORM\Entity]
class Greeting
{
    /**
     * The entity ID
     */
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    /**
     * A nice person
     */
    #[ORM\Column]
    #[Assert\NotBlank]
    public string $name = '';

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

    #[Gedmo\Timestampable(on: 'create')]
    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    protected $createdAt;

    #[Gedmo\Timestampable(on: 'update')]
    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    protected $updatedAt;
}

但这不起作用,所以我尝试使用 Trait

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

/**
 * This is a dummy entity. Remove it!
 */
#[ApiResource]
#[ORM\Entity]
class Greeting
{
    use TimestampableEntity;
    /**
     * The entity ID
     */
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    /**
     * A nice person
     */
    #[ORM\Column]
    #[Assert\NotBlank]
    public string $name = '';

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

在这两种情况下,错误都是:

执行查询时发生异常:SQLSTATE[23000]: 完整性约束违规:1048 列“created_at”不能 空

我过去也尝试过这个对我有用的

# config/services.yaml
    gedmo.listener.timestampable:
        class: Gedmo\Timestampable\TimestampableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ '@annotation_reader' ] ]

错误是:

服务“gedmo.listener.timestampable”依赖于 不存在的服务“annotation_reader”

如果必须在 symfony 6.3 中弃用 Doctrine Lifecycle Subscribers,我不会这样做

编辑 我在探查器(理论)中看到了这一点,所以我猜这 2 个时间戳被设置为

NULL
而不是正确的值

symfony doctrine-orm api-platform.com
1个回答
0
投票

自从升级到 Symfony 6.4 后,我遇到了完全相同的错误。由于不兼容,我必须删除 sensio/framework-extra-bundle。这可能是一个线索吗?

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