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

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

问题描述

我已遵循本教程的说明: 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

config.yml

annotation.listener:
    class: City\AnnotatorBundle\Listener\AnnotationListener
    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 annotaion?您可以使用 @PreUpdate 注释并跳过服务定义。

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

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class YouEntity
{

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    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...

您可以在这里阅读有关Lifecycle回调的更多信息:
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

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

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