<small id='1g682'></small><noframes id='1g682'>

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

  • <legend id='1g682'><style id='1g682'><dir id='1g682'><q id='1g682'></q></dir></style></legend>
          <bdo id='1g682'></bdo><ul id='1g682'></ul>
      1. 如何检查 Doctrine 2 中的实体是否发生了变化?

        How to check if entity changed in Doctrine 2?(如何检查 Doctrine 2 中的实体是否发生了变化?)
          <legend id='ldlKw'><style id='ldlKw'><dir id='ldlKw'><q id='ldlKw'></q></dir></style></legend>
          <tfoot id='ldlKw'></tfoot>

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

            <tbody id='ldlKw'></tbody>

            <i id='ldlKw'><tr id='ldlKw'><dt id='ldlKw'><q id='ldlKw'><span id='ldlKw'><b id='ldlKw'><form id='ldlKw'><ins id='ldlKw'></ins><ul id='ldlKw'></ul><sub id='ldlKw'></sub></form><legend id='ldlKw'></legend><bdo id='ldlKw'><pre id='ldlKw'><center id='ldlKw'></center></pre></bdo></b><th id='ldlKw'></th></span></q></dt></tr></i><div id='ldlKw'><tfoot id='ldlKw'></tfoot><dl id='ldlKw'><fieldset id='ldlKw'></fieldset></dl></div>
                <bdo id='ldlKw'></bdo><ul id='ldlKw'></ul>
                  本文介绍了如何检查 Doctrine 2 中的实体是否发生了变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要检查一个持久化的实体是否已经改变并且需要在数据库上更新.我所做的(但没有奏效)如下:

                  I need to check if a persisted entity has changed and needs to be updated on the database. What I made (and did not work) was the following:

                  $product = $entityManager->getRepository('Product')->find(3);
                  $product->setName('A different name');
                  
                  var_export($entityManager->getUnitOfWork()->isScheduledForUpdate($product));
                  

                  那个代码总是打印false,我在检查工作单元之前也试过flush,但是没有用.

                  That code prints always false, I also tried to flush before check the unit of work, but did not work.

                  有人有什么建议吗?

                  推荐答案

                  我首先要检查您的 setName 函数是否确实在执行某些操作 ($this-> name = $name...) 如果它已经在工作,那么你可以在你的 services.yml 上定义一个在你调用刷新时触发的事件监听器.

                  The first thing I'd check it that your setName function is actually doing something ($this-> name = $name...) If it's already working, then you could define an event listener on your services.yml that is triggered when you call the flush.

                  entity.listener:
                    class: YourNameYourBundleEventListenerEntityListener
                    calls:
                      - [setContainer,  ["@service_container"]]
                    tags:
                      - { name: doctrine.event_listener, event: onFlush }
                  

                  然后定义EntityListener

                  Then you define the EntityListener

                  namespace YourNameYourBundleEventListener;
                  
                  use DoctrineORMEvent;
                  use SymfonyComponentDependencyInjectionContainerAware;
                  
                  class EntityListener extends ContainerAware
                  {   
                  
                      /**
                       * Gets all the entities to flush
                       *
                       * @param EventOnFlushEventArgs $eventArgs Event args
                       */
                      public function onFlush(EventOnFlushEventArgs $eventArgs)
                      {   
                          $em = $eventArgs->getEntityManager();
                          $uow = $em->getUnitOfWork();
                  
                          //Insertions
                          foreach ($uow->getScheduledEntityInsertions() as $entity) {
                              # your code here for the inserted entities
                          }
                  
                          //Updates
                          foreach ($uow->getScheduledEntityUpdates() as $entity) {
                              # your code here for the updated entities
                          }
                  
                          //Deletions
                          foreach ($uow->getScheduledEntityDeletions() as $entity) {
                              # your code here for the deleted entities
                          }
                      }
                  }
                  

                  如果您需要知道哪些实体正在被更改,但是在它们被保存到数据库之后对它们做一些,只需将更改的实体存储在一个私有数组中,然后定义一个从数组中获取实体的 onFlush 事件.

                  If you need to know which entities are being changed, but do something with them after they've been saved to the database, just store the entities changed in a private array, an then define a onFlush event that gets the entities from the array.

                  顺便说一句,要触发此类事件,您需要在实体上添加 @ORMHasLifecycleCallbacks.

                  BTW, to trigger this kind of events you need to add the @ORMHasLifecycleCallbacks on the entity.

                  这篇关于如何检查 Doctrine 2 中的实体是否发生了变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='H0c79'></bdo><ul id='H0c79'></ul>
                        <i id='H0c79'><tr id='H0c79'><dt id='H0c79'><q id='H0c79'><span id='H0c79'><b id='H0c79'><form id='H0c79'><ins id='H0c79'></ins><ul id='H0c79'></ul><sub id='H0c79'></sub></form><legend id='H0c79'></legend><bdo id='H0c79'><pre id='H0c79'><center id='H0c79'></center></pre></bdo></b><th id='H0c79'></th></span></q></dt></tr></i><div id='H0c79'><tfoot id='H0c79'></tfoot><dl id='H0c79'><fieldset id='H0c79'></fieldset></dl></div>
                            <tbody id='H0c79'></tbody>
                        1. <small id='H0c79'></small><noframes id='H0c79'>

                          <tfoot id='H0c79'></tfoot>
                        2. <legend id='H0c79'><style id='H0c79'><dir id='H0c79'><q id='H0c79'></q></dir></style></legend>