简体   繁体   English

在另一个实体被删除\\更新后如何删除\\更新准则2实体

[英]How to remove\update doctrine 2 entities after another entity has been removed\updated

In my particular case I have the following: 在我的特定情况下,我具有以下优势:

I have an entity which at some point may be automatically removed as an orphan. 我有一个实体,在某些时候它可能会自动作为一个孤儿被移除。 Now, I need to react on its removal. 现在,我需要对它的删除做出反应。 Moreover, I do not know exactly if this reaction will or will not involve some doctrine operations . 而且,我不确切知道这种反应是否会涉及某些教义操作。 Particularly, I need to trigger some method on some decoupled component and I do not know this component`s implementation details. 特别是,我需要在某个解耦的组件上触发某种方法,并且我不知道该组件的实现细节。 For example, my default implementation of that component makes use of Doctrine and need to remove some entity when the previously mentioned entity has been removed. 例如,我对该组件的默认实现使用了Doctrine,并且在删除了前面提到的实体时需要删除某个实体。

Now the problem itself: 现在问题本身:

I know that the EntityManager flush operation can not be triggered in the lifecycle events (It is mentioned in the docs, and it is because the lifecycle events occur in the flush method). 我知道EntityManager刷新操作无法在生命周期事件中触发(在文档中已提到,这是因为生命周期事件发生在flush方法中)。 Particularly it can not be triggered in the postRemove event. 特别是无法在postRemove事件中触发它。 So, the problem is that, after some entity removal, I need to trigger some action that may (or may not) trigger doctrine entitymanager flush method but I can not do it in the postRemove event. 因此,问题在于,在删除某些实体之后,我需要触发一些可能(也可能不会)触发教义的entitymanager刷新方法的操作,但是我无法在postRemove事件中执行该操作。 And I do not know any other place where I can do it safely. 而且我不知道有其他地方可以安全地做到这一点。

Ok, finaly I came to the following decision: 好的,最后我做出了以下决定:

In the postRemove action I just add some action (as a simple callable) to the queue. 在postRemove操作中,我仅向队列添加一些操作(作为简单的可调用对象)。 Later on, in the postFlush event I process the queued actions (Its ok, because postFlush event happens after the flush process has been finished). 稍后,在postFlush事件中,我处理排队的动作(没关系,因为postFlush事件在刷新过程完成后发生)。

To implement this in some general way I created a very simple queue manager library ( https://github.com/numesmat/QueueManager ) and a corresponding symfony bundle to wire it up ( https://github.com/numesmat/QueueManagerBundle ). 为了以某种通用的方式实现这一点,我创建了一个非常简单的队列管理器库( https://github.com/numesmat/QueueManager )和一个相应的symfony捆绑包将其连接起来( https://github.com/numesmat/QueueManagerBundle ) 。

Now, to add some postRemove logic I just need to do something like this in my event subscriber: 现在,要添加一些postRemove逻辑,我只需要在事件订阅者中执行以下操作:

public function postRemove(LifecycleEventArgs $args) {
    $queueManager = $this->container->get('arko.queue_manager');
    $queueManager->add(function() {
        // Some logic that can potentially use doctrine flush here
    }, 'my_queue_name');
}

public function postFlush(PostFlushEventArgs $args) {
    $queueManager = $this->container->get('arko.queue_manager');
    $queueManager>process('my_queue_name');
}

In the next few days I gonna write readme to my small queue manager library and bundle. 在接下来的几天里,我将写自述文件到我的小型队列管理器库和捆绑软件中。 Right now, this library or bundle may be installed using composer ( arko/queue-manager and arko/queue-manager-bundle composer packages respectively) and used as simple as described above. 现在,可以使用composer(分别为arko / queue-managerarko / queue-manager-bundle composer软件包)安装此库或捆绑软件,并如上所述进行简单使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM