跟踪 Doctrine 实体的字段变化 [英] Track field changes on Doctrine entity

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

问题描述

我想跟踪对 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.

到目前为止,我有一个订阅 preUpdateEventSubscriber.在这里,我想创建一个新实体,它存储新旧值并保存对更新的实体的引用.

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.

问题是,我找不到持久化这个新实体的方法.如果我在 preUpdatepersist()postUpdate 中的 flush(),如果我只改变一个实体.如果更改了多个实体,我会收到更改集为空的错误.

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 (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('YourNameSpaceYourBundleEntityYourChangeTrackingEntity');
                    $unitOfWork->computeChangeSet($metaData, $yourChangeTrackingEntity);
                }
            }
        }
    }
}

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

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