Doctrine 生命周期回调prePersist未触发(使用YAML文件)

Doctrine 生命周期回调prePersist未触发(使用YAML文件),doctrine,symfony,Doctrine,Symfony,我在学Symfony,跟着书学。在本教程中,我成功地配置了prePersist事件(在插入时设置createdAt字段) 现在我也在尝试做同样的事情,但是使用YAML文件而不是注释。这里是我的orm.yml文件: AppBundle\Entity\Chronicle: type: entity table: chronicles id: id: type: integer generator: {strate

我在学Symfony,跟着书学。在本教程中,我成功地配置了prePersist事件(在插入时设置createdAt字段)

现在我也在尝试做同样的事情,但是使用YAML文件而不是注释。这里是我的orm.yml文件:

AppBundle\Entity\Chronicle:
    type: entity
    table: chronicles
    id:
        id:
            type: integer
            generator: {strategy: AUTO}
    fields:
        name:
            type: string
            length: 256
        createdAt:
            type: datetime

    manyToOne:
        creator: 
            targetEntity: User
            inversedBy: chronicles
            joinColumn: 
                name: user_id
                referencedColumnName: id
        game:
            targetEntity: Game
            joinColumn:
                name: game_id
                referencedColumnName: id

    oneToMany:
        characters:
            targetEntity: Character
            mappedBy: chronicle

    lifeCycleCallbacks:
        prePersist: [ setCreatedAtValue ]
这是我的实体类的一个片段:

class Chronicle
{
    private $id;

    private $name;
    private $createdAt;

    // Associations
    private $game;
    private $creator;
    private $characters;
    // TODO: users or user relationships

    // Constructor
    public function __construct(User $creator=null) {
        $this->characters = new ArrayCollection();
        $this->creator = $creator;
    }

    /**
    * Set createdAt at the current time.
    *
    * (Lifecycle callback)
    */
    public function setCreatedAtValue() 
    {
        $this->createdAt = new \DateTime();
    }
    // ...
}
但是从未调用setCreatedAtValue()方法。因此,当我尝试插入一个对象时,会出现一个异常

我注意到,在使用注释时,我必须告诉您是否存在带有@ORM\HasLifecycleCallbacks()注释的生命周期回调,但我没有找到任何与yml中的回调等效的方法,或者如果需要的话


在一个教程中,我发现我应该在services.yml中注册回调,但该教程从未提到过它,而且我在其他任何地方都没有找到过它。

如果将“lifeCycleCallbacks”更改为“lifeCycleCallbacks”,它将被调用。

如果将“lifeCycleCallbacks”更改为“lifeCycleCallbacks”,它将被调用