<tfoot id='ZJvBf'></tfoot>
  • <small id='ZJvBf'></small><noframes id='ZJvBf'>

    <i id='ZJvBf'><tr id='ZJvBf'><dt id='ZJvBf'><q id='ZJvBf'><span id='ZJvBf'><b id='ZJvBf'><form id='ZJvBf'><ins id='ZJvBf'></ins><ul id='ZJvBf'></ul><sub id='ZJvBf'></sub></form><legend id='ZJvBf'></legend><bdo id='ZJvBf'><pre id='ZJvBf'><center id='ZJvBf'></center></pre></bdo></b><th id='ZJvBf'></th></span></q></dt></tr></i><div id='ZJvBf'><tfoot id='ZJvBf'></tfoot><dl id='ZJvBf'><fieldset id='ZJvBf'></fieldset></dl></div>
    1. <legend id='ZJvBf'><style id='ZJvBf'><dir id='ZJvBf'><q id='ZJvBf'></q></dir></style></legend>

          <bdo id='ZJvBf'></bdo><ul id='ZJvBf'></ul>

        跟踪 Doctrine 实体的字段变化

        Track field changes on Doctrine entity(跟踪 Doctrine 实体的字段变化)
          <tbody id='hb3gm'></tbody>
        <i id='hb3gm'><tr id='hb3gm'><dt id='hb3gm'><q id='hb3gm'><span id='hb3gm'><b id='hb3gm'><form id='hb3gm'><ins id='hb3gm'></ins><ul id='hb3gm'></ul><sub id='hb3gm'></sub></form><legend id='hb3gm'></legend><bdo id='hb3gm'><pre id='hb3gm'><center id='hb3gm'></center></pre></bdo></b><th id='hb3gm'></th></span></q></dt></tr></i><div id='hb3gm'><tfoot id='hb3gm'></tfoot><dl id='hb3gm'><fieldset id='hb3gm'></fieldset></dl></div>
          <bdo id='hb3gm'></bdo><ul id='hb3gm'></ul>
          <tfoot id='hb3gm'></tfoot>

          <small id='hb3gm'></small><noframes id='hb3gm'>

                • <legend id='hb3gm'><style id='hb3gm'><dir id='hb3gm'><q id='hb3gm'></q></dir></style></legend>
                • 本文介绍了跟踪 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 实体的字段变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)
                    <i id='dYon3'><tr id='dYon3'><dt id='dYon3'><q id='dYon3'><span id='dYon3'><b id='dYon3'><form id='dYon3'><ins id='dYon3'></ins><ul id='dYon3'></ul><sub id='dYon3'></sub></form><legend id='dYon3'></legend><bdo id='dYon3'><pre id='dYon3'><center id='dYon3'></center></pre></bdo></b><th id='dYon3'></th></span></q></dt></tr></i><div id='dYon3'><tfoot id='dYon3'></tfoot><dl id='dYon3'><fieldset id='dYon3'></fieldset></dl></div>

                    <small id='dYon3'></small><noframes id='dYon3'>

                      <tbody id='dYon3'></tbody>
                      • <legend id='dYon3'><style id='dYon3'><dir id='dYon3'><q id='dYon3'></q></dir></style></legend><tfoot id='dYon3'></tfoot>

                            <bdo id='dYon3'></bdo><ul id='dYon3'></ul>