SonataAdminBundle - check changes in `preUpdate` hook

坚强是说给别人听的谎言 提交于 2019-12-19 06:22:05

问题


Is it possible to check if field was changed on preUpdate hook? I'm looking for something like preUpdate hasChangedField($fieldName) Doctrine functionality. Any ideas?


回答1:


This question is a bit similar to this one

Your solution is just to compare the field of the old object with the new one and see where it differs.

So for example:

public function preUpdate($newObject)
{
    $em = $this->getModelManager()->getEntityManager($this->getClass());
    $originalObject = $em->getUnitOfWork()->getOriginalEntityData($newObject);

    if ($newObject->getSomeField() !== $originalObject['fieldName']) {
        // Field has been changed
    }
}



回答2:


For me the best approach is this in Sonata Admin:

$newField = $this->getForm()->get('field')->getData();
$oldField = $this->getForm()->get('field')->getConfig()->getData();

You shouldn't use unit of work unless there is no option. Also, if you have a not mapped field, you can't access it by entity object.

In a normal Doctrine lyfe cycle event, the best option is Doctrine preupdate event doc



来源:https://stackoverflow.com/questions/21556895/sonataadminbundle-check-changes-in-preupdate-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!