Symfony 2从实体管理器获取实体的原始数据

问题描述 投票:10回答:3

我正在使用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
3个回答
25
投票

您可以通过获取学说的工作单元来访问原始数据。如文档所示

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

从工作单元中获取密码并在您的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


0
投票

在实体管理中重置实体,例如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);

            }
        }
    }

-3
投票
$this->getConfigurationPool()
     ->getContainer()
     ->get('Doctrine')
     ->getRepository("...")
     ->find(id here);

所以省略getManager()部分;

© www.soinside.com 2019 - 2024. All rights reserved.