Doctrine2的PreUpdate在Symfony2中缺少PreUpdateEventArgs参数 [英] Doctrine2's preUpdate missing PreUpdateEventArgs argument in Symfony2

查看:99
本文介绍了Doctrine2的PreUpdate在Symfony2中缺少PreUpdateEventArgs参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Doctrine2文档 preUpdate 事件

Doctrine2 documentation on preUpdate event says


这个事件有一个强大的功能, PreUpdateEventArgs 实例,其中包含对该实体的计算变更集的引用。这意味着您可以访问使用其旧值和新值为该实体更改的所有字段。

This event has a powerful feature however, it is executed with a PreUpdateEventArgs instance, which contains a reference to the computed change-set of this entity. This means you have access to all the fields that have changed for this entity with their old and new value.

声音很有用!所以我做了什么:

Sounds useful! So what I do:

/**
 * Acme\TestBundle\Entity\Article
 *
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Article
{
    // ...

    /**
     * @ORM\PreUpdate
     */
    public function preUpdate(\Doctrine\ORM\Event\PreUpdateEventArgs $eventArgs)
    {
        if ( /* use $eventArgs here */ )

            $this->updatedAt = new \DateTime();
    }

    // ...
}

但没有运气 - 没有参数通过:

But no luck - no arguments are passed:


可追踪的致命错误:参数1传递到

Acme\TestBundle\Entity\Article :: preUpdate()必须是

Doctrine\ORM\的实例事件\PreUpdateEventArgs 无给定,在

... \vendor\doctrine\lib\\在线1540上定义了\\Doctrine\ORM\Mapping\ClassMetadataInfo.php ,并在 ... \src\Acme\TestBundle\Entity\Article .php line 163

Catchable Fatal Error: Argument 1 passed to
Acme\TestBundle\Entity\Article::preUpdate() must be an instance of
Doctrine\ORM\Event\PreUpdateEventArgs, none given, called in
...\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataInfo.php on line 1540 and defined in ...\src\Acme\TestBundle\Entity\Article.php line 163

我想这必须在Symfony2中以其他方式工作。我如何做?

I guess this must work some other way in Symfony2. How do I do it?

推荐答案

有两种处理生命周期事件的方法。

There are two ways of handling lifecycle events.

首先简单地将您的教义实体的方法注释为@ ORM\PreUpdate。更新前将调用此方法。第二个更复杂。它涉及听众
preUpdate 。这是一个单独的课程,您订阅听特定的事件。

First is simply annotate a method of your doctrine entity as @ORM\PreUpdate. This method will be called before update. The second is more complicated. It involves listener preUpdate. It's a separate class that you subscribe to listen to particular events.

仔细阅读 here 和symfony 食谱

Read carefully from here again and the symfony cookbook.

这篇关于Doctrine2的PreUpdate在Symfony2中缺少PreUpdateEventArgs参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆