Doctrine EventSubscriber

时间:2016-05-03 14:12:36

标签: doctrine-orm

在我的控制器中,我更新了实体中的Lastname

public function testAction(){
        $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        $entity = $objectManager->getRepository('Profiling\Entity\Profile')->find(18);
        $entity->setLastname('A');
        $objectManager->flush();
        return $this->response;
    }

和我的EventSubscriber

public function onFlush(OnFlushEventArgs $args)
{
    $em  = $args->getEntityManager();
    $uow = $em->getUnitOfWork();
    foreach ($uow->getScheduledEntityUpdates() as $updated) {

        if ($updated instanceof Profile) {
            $updated->setBalancelog($updated->getBalance());
            echo $updated->getLastname();
        }
    }

    foreach ($uow->getScheduledEntityInsertions() as $insertion) {
        if ($insertion instanceof Profile) {
            $profile = $insertion;
            $profile->setBalancelog($profile->getBalance());
        }
    }


$uow->computeChangeSets();

}

它在我的活动订阅者中回应“A”,但它没有得到保存。我错过了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

由recomputeSingleEntityChangeSet

修复
if ($updated instanceof Profile) {
            $updated->setBalancelog($updated->getBalance());

            $uow->recomputeSingleEntityChangeSet($em->getClassMetadata('Profiling\Entity\Profile'),$updated);
            echo $updated->getLastname();
        }