Track field changes on Doctrine entity(跟踪 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.
到目前为止,我有一个订阅 preUpdate
的 EventSubscriber
.在这里,我想创建一个新实体,它存储新旧值并保存对更新的实体的引用.
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
中 persist()
和 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 实体的字段变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:跟踪 Doctrine 实体的字段变化
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01