跟踪理论实体的领域变化 [英] Track field changes on Doctrine entity

查看:75
本文介绍了跟踪理论实体的领域变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪一个Doctrine实体的一个字段的变化。我使用Symfony 2.5.0和Doctrine 2.2.3。

I want to track changes to a field of a Doctrine Entity. I use Symfony 2.5.0 and Doctrine 2.2.3.

到目前为止,我有一个 EventSubscriber 订阅更新前。在这里,我想创建一个新的Entity,它存储新的和旧的值,并持有对更新的Entity的引用。

So far i have an EventSubscriber that subscribes to preUpdate. Here I want to create a new Entity which stores the new and old value and holds a reference to the Entity that is updated.

问题是,我不能找到一种方法来坚持这个新的实体。如果我在 preUpdate flush()中的 persist() postUpdate 中,如果我只更改一个实体,它将起作用。如果多个实体被更改,我会收到一个错误,这个变更集是空的。

The problem is, that I can't find a way to persist this new Entity. If I persist() in preUpdate and flush() in postUpdate, it works if I change only one Entity. If multiple Entities are changed, I get an error that the changeset is empty.

我尝试着使用不同的事件,不同的结果。

I tried fiddling with different events with different results. Blank pages, tracking entites do not get persisted etc.

我认为这应该是一个常见的用例 - 但我找不到例子。

I think that this should be a common use case - but I can't find examples.

推荐答案

不要使用preUpdate或postUpdate,您将遇到问题。请查看 onFlush

Don't use preUpdate or postUpdate, you will have problems. Take a look at onFlush instead.

此时您可以访问完整的更改集,因此您可以了解哪些字段已更改,添加了哪些内容。您还可以安全地保留新实体。请注意, docs 说,您将不得不重新计算当您坚持或更改实体时,更改集。

You have access to the complete changeset at this point, so you can find out what fields have changed, what's been added etc. You can also safely persist new entities. Note as the docs say, you will have to recompute change sets when you persist or change entities.

简单的例子我敲了敲,没有测试,但是类似的东西会让你想要的。

Simple example I knocked together, not tested but something similar to this will get you what you want.

public function onFlush(OnFlushEventArgs $args) {

    $entityManager = $args->getEntityManager();
    $unitOfWork = $entityManager->getUnitOfWork();
    $updatedEntities = $unitOfWork->getScheduledEntityUpdates();

    foreach ($updatedEntities as $updatedEntity) {

        if ($updatedEntity instanceof YourEntity) {

            $changeset = $unitOfWork->getEntityChangeSet($updatedEntity);

            if (!is_array($changeset)) {

                return null;
            }

            if (array_key_exists('someFieldInYourEntity', $changeset)) {

                $changes = $changeset['someFieldInYourEntity'];

                $previousValueForField = array_key_exists(0, $changes) ? $changes[0] : null;
                $newValueForField = array_key_exists(1, $changes) ? $changes[1] : null;

                if ($previousValueForField != $newValueForField) {

                    $yourChangeTrackingEntity = new YourChangeTrackingEntity();
                    $yourChangeTrackingEntity->setSomeFieldChanged($previousValueForField);
                    $yourChangeTrackingEntity->setSomeFieldChangedTo($newValueForField);

                    $entityManager->persist($yourChangeTrackingEntity);
                    $metaData = $entityManager->getClassMetadata('YourNameSpace\YourBundle\Entity\YourChangeTrackingEntity');
                    $unitOfWork->computeChangeSet($metaData, $yourChangeTrackingEntity);
                }
            }
        }
    }
}

这篇关于跟踪理论实体的领域变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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