在 Doctrine 2 上未触发 preUpdate 和 postUpdate 事件 [英] preUpdate and postUpdate events not triggered on Doctrine 2

查看:11
本文介绍了在 Doctrine 2 上未触发 preUpdate 和 postUpdate 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照本教程中的说明进行操作:http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html,并创建了一个简单的侦听器,用于侦听 Doctrine 在插入或更新实体时调度的事件.preInsert 和 postInsert 事件工作正常,并在创建新实体时分派.但是,无论如何,在实体的更新上都不会调用 preUpdate 和 postUpdate.onFlush 也是如此.作为旁注,我有一个控制台生成的控制器,它支持基本的 CRUD 操作,并且保持不变.

I have followed the instructions from this tutorial: http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html, and have created a simple listener, that listens for events dispatched by Doctrine on insert or update of an entity. The preInsert and the postInsert events work fine and are dispatched on the creation of a new entity. However, preUpdate and postUpdate are never called on the update of the entity no matter what. The same goes for onFlush. As a side note, I have a console generated controller that supports the basic CRUD operations, and have left it untouched.

下面是一些代码片段来演示我这样做的方式.

Below are some code snippets to demonstrate the way I am doing this.

config.yml

annotation.listener:
    class: CityAnnotatorBundleListenerAnnotationListener
    tags:
        -  { name: doctrine.event_listener, event: postUpdate}

监听器实现(为了简单起见,我省略了其他函数,只留下了 postUpdate)

Listener implementation (I have omitted the other functions and left only the postUpdate for simplicity purposes)

class AnnotationListener
{

    public function postUpdate(LifecycleEventArgs $args)
    {
        $entity=$args->getEntity();

        echo $entity->getId();
        die;
    }
}

实体 id 永远不会显示,并且脚本会继续执行直到它完成,尽管函数末尾有 die.

The entity id is never displayed, and the script continues its execution until it is complete, despite the die at the end of the function.

推荐答案

是不是忘记添加@HasLifecycleCallbacks注解了?您可以使用 @PreUpdate 注释并完全跳过服务定义.

Did you forget to add @HasLifecycleCallbacks annotaion? You could use @PreUpdate annotation and skip service definition altogether.

/**
 * @ORMEntity
 * @ORMHasLifecycleCallbacks
 */
class YouEntity
{

    /**
     * @ORMPrePersist()
     * @ORMPreUpdate()
     */
    public function preUpdate(){
        // .... your pre-update logic here
    }
    ....
}

在我看来,这种附加事件的方式要容易得多,因为您不必明确定义新的服务和侦听器.您还可以直接访问正在更新的数据,因为此方法是您实体中的位置.

In my opinion this way of attaching events is much easier as you don't have to define new services and listeners explicitly. Also you have direct access to data being updated as this method is locations within your entity.

现在,缺点是您将逻辑与模型混合在一起,如果可能的话,应该避免这种情况......

Now, drawback is that you mix logic with your model and that's something that should be avoided if possible...

您可以在此处阅读有关生命周期回调的更多信息:http://symfony.com/doc/master/cookbook/doctrine/file_uploads.html#using-lifecycle-callbacks

You can read more about Lifecycle callbacks here: http://symfony.com/doc/master/cookbook/doctrine/file_uploads.html#using-lifecycle-callbacks

这篇关于在 Doctrine 2 上未触发 preUpdate 和 postUpdate 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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