Symfony 2 Get original data of entity from entity manager Symfony 2 Get original data of entity from entity manager symfony symfony

Symfony 2 Get original data of entity from entity manager


You can access the original data by getting doctrine's Unit of Work.As from docs

You can get direct access to the Unit of Work by callingEntityManager#getUnitOfWork(). This will return the UnitOfWorkinstance the EntityManager is currently using.An array containing theoriginal data of entity

Grab the password from Unit of Work and use in your setter method

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 */    }}

Hope it works fine

Direct access to a Unit of Work


Reset entity in entity manage, example for onFlush event

  /**     * @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);

So leave out the getManager() part;