【问题标题】:Symfony 2 Get original data of entity from entity managerSymfony 2 从实体管理器获取实体的原始数据
【发布时间】:2013-12-13 15:28:11
【问题描述】:

我正在为我的应用程序使用 Sonata 管理包,一切正常,在我的应用程序中,我有用户和管理员,管理员可以在我尝试更新用户时添加/编辑/删除用户密码数据有问题从用户表中覆盖。我已经覆盖了管理控制器的preUpdate 方法,我得到了$object,它有一个用户实体管理器的实例,所以如果用户离开以更新密码并保存数据,密码就会丢失。

public function preUpdate($object)
{
    $Password = $object->getUserPassword();
    if (!empty($Password)) { /* i check here if user has enter password then update it goes well*/
        $salt = md5(time());
        $encoderservice = $this->getConfigurationPool()->getContainer()->get('security.encoder_factory');
        $User = new User();
        $encoder = $encoderservice->getEncoder($User);
        $encoded_pass = $encoder->encodePassword($Password, $salt);
        $object->setUserSalt($salt)->setUserPassword($encoded_pass);
    } else { /* here i try to set the old password if user not enters the new password but fails */
        $object->setUserPassword($object->getUserPassword());
    }
}

当我尝试设置$object->setUserPassword($object->getUserPassword()); 时,它会变为 null 并将密码更新为 null 它没有获得编辑数据我试图再次获取存储库(如下)以获取密码但运气不一样

$DM = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager()->getRepository("...")->find(id here);

有没有办法在实体管理器中访问当前实体的原始数据

【问题讨论】:

    标签: php symfony doctrine-orm sonata-admin sonata


    【解决方案1】:

    您可以通过获取学说的工作单元来访问原始数据。As from docs

    您可以通过调用直接访问工作单元 EntityManager#getUnitOfWork()。这将返回 UnitOfWork EntityManager 当前正在使用的实例。包含 实体原始数据

    从 Unit of Work 获取密码并在您的 setter 方法中使用

    public function preUpdate($object)
    {
    
        $DM = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
        $uow = $DM->getUnitOfWork();
        $OriginalEntityData = $uow->getOriginalEntityData( $object );
        $Password = $object->getUserPassword();
        if (!empty($Password)) { /* i check here if user has enter password then update it goes well*/
            $salt = md5(time());
            $encoderservice = $this->getConfigurationPool()->getContainer()->get('security.encoder_factory');
            $User = new User();
            $encoder = $encoderservice->getEncoder($User);
            $encoded_pass = $encoder->encodePassword($Password, $salt);
            $object->setUserSalt($salt)->setUserPassword($encoded_pass);
        } else { /* here i try to set the old password if user not enters the new password but fails */
            $object->setUserPassword($OriginalEntityData['Password']);/* your property name for password field */
        }
    }
    

    希望一切顺利

    Direct access to a Unit of Work

    【讨论】:

    • 链接失效
    【解决方案2】:
    $this->getConfigurationPool()
         ->getContainer()
         ->get('Doctrine')
         ->getRepository("...")
         ->find(id here);
    

    所以省略getManager() 部分;

    【讨论】:

      【解决方案3】:

      在实体管理中重置实体,例如onFlush事件

        /**
           * @param OnFlushEventArgs $args
           *
           * @throws \Doctrine\ORM\ORMException
           * @throws \Doctrine\ORM\OptimisticLockException
           */
          public function onFlush(OnFlushEventArgs  $args)
          {
              $em = $args->getEntityManager();
              $uow = $em->getUnitOfWork();
      
              foreach ($uow->getScheduledEntityUpdates() as $keyEntity => $entity) {
                  if ($entity instanceof Bill) {
                      $em->refresh($entity);
                      $this->createPdfs($entity);
      
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多