问题描述
我想跟踪对 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 实体的字段变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)