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

  1. <legend id='Z0mJM'><style id='Z0mJM'><dir id='Z0mJM'><q id='Z0mJM'></q></dir></style></legend>

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

    <tfoot id='Z0mJM'></tfoot>

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

      如何判断当前交易是否会改变 Doctrine 2 的任何实体?

      时间:2023-08-18

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

    1. <legend id='BWYEz'><style id='BWYEz'><dir id='BWYEz'><q id='BWYEz'></q></dir></style></legend>

          • <bdo id='BWYEz'></bdo><ul id='BWYEz'></ul>
              <tbody id='BWYEz'></tbody>

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

              1. 本文介绍了如何判断当前交易是否会改变 Doctrine 2 的任何实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 Doctrine 来保存用户数据,并且我想要一个 last modified 字段.这是用户按下 Save 后我希望如何保存表单的伪代码:

                I'm using Doctrine to save user data and I want to have a last modification field. Here is the pseudo-code for how I would like to save the form once the user presses Save:

                • 开始交易
                • 做很多事情,可能是查询数据库,也可能不是
                • 如果此交易有任何改变
                  • 修改 last updated 字段

                  有问题的部分是这个交易是否会改变任何东西.Doctrine 可以给我这样的信息吗?

                  The problematic part is if anything will be changed by this transaction. Can Doctrine give me such information?

                  如何判断当前事务中的实体是否发生了变化?

                  How can I tell if entities have changed in the current transaction?

                  编辑

                  为了解决问题,我试图修改名为 User 的实体中名为 lastUpdated 的字段,如果有任何实体(包括但不限于 User) 将在当前事务提交后更改.换句话说,如果我开始交易并修改名为 Garage 的实体的名为 nbCars 的字段,我希望更新的 lastUpdated 字段User 实体,即使该实体尚未修改.

                  Just to clear things up, I'm trying to modify a field called lastUpdated in an entity called User if any entity (including but not limited to User) will be changed once the currect transaction is commited. In other words, if I start a transaction and modify the field called nbCars of an entity called Garage, I wish to update the lastUpdated field of the User entity even though that entity hasn't been modified.

                  推荐答案

                  这是一个必要的回复,旨在更正@ColinMorelli 发布的内容(因为不允许在生命周期事件侦听器中刷新 - 是的,文档中有一个位置否则说,但我们会摆脱它,所以请不要这样做!).

                  This is a necessary reply that aims at correcting what @ColinMorelli posted (since flushing within an lifecycle event listener is disallowed - yes, there's one location in the docs that says otherwise, but we'll get rid of that, so please don't do it!).

                  你可以简单地听onFlush 带有如下监听器:

                  You can simply listen to onFlush with a listener like following:

                  use DoctrineCommonEventSubscriber;
                  use DoctrineORMEventOnFlushEventArgs;
                  use DoctrineORMEvents;
                  
                  class UpdateUserEventSubscriber implements EventSubscriber
                  {
                      protected $user;
                  
                      public function __construct(User $user)
                      {
                          // assuming the user is managed here
                          $this->user = $user;
                      }
                  
                      public function onFlush(OnFlushEventArgs $args)
                      {
                          $em  = $args->getEntityManager();
                          $uow = $em->getUnitOfWork();
                  
                          // before you ask, `(bool) array()` with empty array is `false`
                          if (
                              $uow->getScheduledEntityInsertions()
                              || $uow->getScheduledEntityUpdates()
                              || $uow->getScheduledEntityDeletions()
                              || $uow->getScheduledCollectionUpdates()
                              || $uow->getScheduledCollectionDeletions()
                          ) {
                              // update the user here
                              $this->user->setLastModifiedDate(new DateTime());
                              $uow->recomputeSingleEntityChangeSet(
                                  $em->getClassMetadata(get_class($this->user)), 
                                  $this->user
                              );
                          }
                      }
                  
                      public function getSubscribedEvents()
                      {
                          return array(Events::onFlush);
                      }
                  }
                  

                  仅当 UnitOfWork 包含要提交到数据库的更改时,这才会将更改应用于配置的 User 对象(工作单元实际上是您可能的定义为应用级状态事务).

                  This will apply the change to the configured User object only if the UnitOfWork contains changes to be committed to the DB (an unit of work is actually what you could probably define as an application level state transaction).

                  您可以随时通过调用在 ORM 中注册此订阅者

                  You can register this subscriber with the ORM at any time by calling

                  $user         = $entityManager->find('User', 123);
                  $eventManager = $entityManager->getEventManager();
                  $subscriber   = new UpdateUserEventSubscriber($user);
                  
                  $eventManager->addEventSubscriber($subscriber);
                  

                  这篇关于如何判断当前交易是否会改变 Doctrine 2 的任何实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Doctrine2.3 和 OneToOne 级联持续似乎不起作用 下一篇:对教义的 @OneToMany ArrayCollection 进行排序

                相关文章

                  <legend id='YEWY5'><style id='YEWY5'><dir id='YEWY5'><q id='YEWY5'></q></dir></style></legend>
                1. <tfoot id='YEWY5'></tfoot>

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

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