• <small id='KHP1x'></small><noframes id='KHP1x'>

  • <legend id='KHP1x'><style id='KHP1x'><dir id='KHP1x'><q id='KHP1x'></q></dir></style></legend>

      <tfoot id='KHP1x'></tfoot>

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

        • <bdo id='KHP1x'></bdo><ul id='KHP1x'></ul>

        如何在 Doctrine2 中访问 PrePersist LifecycleCallback 上的旧值

        时间:2024-08-15
      1. <small id='cZavH'></small><noframes id='cZavH'>

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

                  本文介绍了如何在 Doctrine2 中访问 PrePersist LifecycleCallback 上的旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Doctrine2 中有一个实体,并将 HasLivecycleCallbacks 与 PrePersist 一起使用.一般来说,这很好用,但我只想在实体中的某些字段发生更改时更改版本.我有机会获得旧价值观吗?还是只是更改了键?

                  I have an entity in Doctrine2 and use the HasLivecycleCallbacks with PrePersist. In general this works fine, but I would like to change the version only, when certain fields in my entity change. Do I have a chance to get the old Values? Or just the keys that have been changed?

                  /**
                   * @ORMHasLifecycleCallbacks
                   */
                  class Person {
                  
                  
                      /**
                       * @PrePersist
                       * @PreUpdate
                       */
                      public function increaseVersion() {
                  
                  
                              if ( $this->version == null ) {
                                  $this->version = 0;
                              }
                              // only do this, when a certain attribute changed
                              $this->version++;
                      }
                  }
                  

                  推荐答案

                  这取决于我们说的是哪个 LifecycleEvent.PrePersist 和 PreUpdate 是不同的事件.

                  It depends on which LifecycleEvent we are talking about. PrePersist and PreUpdate are different events.

                  PreUpdate 在实体更新之前触发.这会给你一个 PreUpdateEventArgs 对象,它是一个扩展的 LifecycleEventArgs 对象.这将允许您查询更改的字段并允许您访问旧值和新值:

                  PreUpdate is fired before an Entity is, well, updated. This will give you a PreUpdateEventArgs object, which is an extended LifecycleEventArgs object. This will allow you to query for changed fields and give you access to the old and new value:

                  if ($event->hasChangedField('foo')) {
                      $oldValue = $event->getOldValue('foo');
                      $newValue = $event->getNewValue('foo');
                  }
                  

                  您还可以通过 getEntityChangeSet() 获取所有更改的字段值,这将为您提供如下数组:

                  You could also get all the changed field values through getEntityChangeSet(), which would give you an array like this:

                  array(
                      'foo' => array(
                          0 => 'oldValue',
                          1 => 'newValue'
                      ),
                      // more changed fields (if any) …
                  )
                  

                  另一方面,

                  PrePersist 假设一个新实体(想想插入新行).在 PrePersist 中,你会得到一个 LifecycleEventArgs 对象,它只能访问实体和 EntityManager.理论上,您可以访问 UnitOfWork(它跟踪对实体的所有更改)通过 EntityManager,所以你可以尝试这样做

                  PrePersist, on the other hand, assumes a fresh Entity (think insert new row). In PrePersist, you'll get a LifecycleEventArgs object which only has access to the Entity and the EntityManager. In theory, you can get access to the UnitOfWork (which keeps track of all the changes to Entities) through the EntityManager, so you could try to do

                  $changeSet = $event->getEntityManager()->getUnitOfWork()->getEntityChangeSet(
                      $event->getEntity()
                  );
                  

                  获取要持久化实体的更改.然后,您可以检查此数组是否有更改的字段.但是,由于我们谈论的是插入而不是更新,我假设所有字段都将被视为已更改",并且旧值可能全部为空.我不确定,这会根据您的需要工作.

                  to get the changes for the to be persisted Entity. You could then check this array for changed fields. However, since we are talking about an insert and not an update, I assume all fields wil be considered "changed" and the old values will likely be all null. I am not sure, this will work as you need it.

                  进一步参考:http://docs.doctrine-project.org/en/2.0.x/reference/events.html

                  这篇关于如何在 Doctrine2 中访问 PrePersist LifecycleCallback 上的旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Doctrine2 将实体导出到数组 下一篇:如何在 Doctrine 的 Mongodb ODM 中使用正则表达式?

                  相关文章

                    <bdo id='7TaoK'></bdo><ul id='7TaoK'></ul>
                  <tfoot id='7TaoK'></tfoot>

                    <legend id='7TaoK'><style id='7TaoK'><dir id='7TaoK'><q id='7TaoK'></q></dir></style></legend>

                    1. <small id='7TaoK'></small><noframes id='7TaoK'>

                      <i id='7TaoK'><tr id='7TaoK'><dt id='7TaoK'><q id='7TaoK'><span id='7TaoK'><b id='7TaoK'><form id='7TaoK'><ins id='7TaoK'></ins><ul id='7TaoK'></ul><sub id='7TaoK'></sub></form><legend id='7TaoK'></legend><bdo id='7TaoK'><pre id='7TaoK'><center id='7TaoK'></center></pre></bdo></b><th id='7TaoK'></th></span></q></dt></tr></i><div id='7TaoK'><tfoot id='7TaoK'></tfoot><dl id='7TaoK'><fieldset id='7TaoK'></fieldset></dl></div>