简体   繁体   English

无法在preUpdate奏鸣曲中获取我的旧实体

[英]Can't get my old Entity in preUpdate Sonata

Here's the deal, i'm trying to update an Entity, but i'd like to compare the state of my entity before the update with the state of my entity after the update. 这是交易,我正在尝试更新实体,但是我想将更新之前的实体状态与更新之后的实体状态进行比较。

I try to compare the number of Users in my entity. 我尝试比较实体中的用户数。

For example, if i add a User, i want my code to know that, for example, i had 4 Users before update, and 5 after (and get that User for further use). 例如,如果我添加一个用户,我想让我的代码知道,例如,我在更新之前有4个用户,在更新之后有5个用户(并获得该用户以供进一步使用)。

After reading different topics, i've tried doing like that in my preUpdate($object){} method 阅读了不同的主题之后,我尝试在preUpdate($object){}方法中这样做

$em = this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
$original = $em->getUnitOfWork()->getOriginalEntityData($object);

But both 但是两者

var_dump(count($object->getUsers()));
var_dump(count($original['users']));

Give the same value, and, according to my example, the value is 5 in both case (so the value after the update). 给出相同的值,根据我的示例,两种情况下的值均为5(因此更新后的值)。

Is there a way i can save the old_state of my entity in a var? 有什么方法可以将实体的old_state保存在var中? What am i doing wrong? 我究竟做错了什么?

EDIT: It's not the preUpdate function of Doctrine, but the preUpdate function of SonataAdmin, don't know if they're the same. 编辑:这不是Doctrine的preUpdate函数,而是SonataAdmin的preUpdate函数,不知道它们是否相同。

Please try this in the preUpdate function: 请在preUpdate函数中尝试以下操作:

    $em = $this->getModelManager()->getEntityManager($this->getClass());
    $original = $em->getUnitOfWork()->getOriginalEntityData($object);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM