• <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 上的旧值

        How to access old values on PrePersist LifecycleCallback in Doctrine2(如何在 Doctrine2 中访问 PrePersist LifecycleCallback 上的旧值)
      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 上的旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <bdo id='7TaoK'></bdo><ul id='7TaoK'></ul>
                      <tbody id='7TaoK'></tbody>
                    <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>