SonataAdminBundle - check changes in `preUpdate` hook

后端 未结 2 1911
盖世英雄少女心
盖世英雄少女心 2021-01-12 08:11

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

2条回答
  •  逝去的感伤
    2021-01-12 08:54

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

提交回复
热议问题