我的应用程序使用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);
有没有一种方法可以访问实体管理器中当前实体的原始数据
您可以通过获取条令的工作单元来访问原始数据
您可以通过拨打EntityManager#getUnitOfWork()。这将返回UnitOfWorkEntityManager当前正在使用的实例。包含实体的原始数据
从工作单元获取密码,并在您的设置方法中使用
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 */
}
}
希望它运行良好
直接访问工作单元
在实体管理中重置实体,例如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);
}
}
}
$this->getConfigurationPool()
->getContainer()
->get('Doctrine')
->getRepository("...")
->find(id here);
所以省略getManager()
部分;