Find out field change after update in Symfony 2.x

时间:2017-08-14 06:07:04

标签: symfony

I want to perform an action when a field changes from null to any value. cat /tmp/tag.log will only show preupdate. It continues to run without stopping.

This is what I have done.

    public function preUpdate(PreUpdateEventArgs $args)
    {
        if ($args->getEntity() instanceof Contact) {
           file_put_contents('/tmp/tag.log', 'preUpdate'  .PHP_EOL, FILE_APPEND);

           if ($args->hasChangedField('assignedTo')){
              file_put_contents('/tmp/tag.log', 'preUpdate changed'  .PHP_EOL, FILE_APPEND);
           }
           if ( $args->getOldValue('assignedTo') == null ){
              file_put_contents('/tmp/tag.log', 'preUpdate assignedTo is null '  .PHP_EOL, FILE_APPEND);
           }
           if ( $args->getNewValue('assignedTo') != null) {
              file_put_contents('/tmp/tag.log', 'preUpdate changed  to :'.$args->getNewValue('assignedTo') . PHP_EOL, FILE_APPEND);
           }
        }
    }

I have registered this service with

          tags:
       - { name: doctrine.event_listener, event: preUpdate }

1 个答案:

答案 0 :(得分:0)

问题在于字段名称。正确的字段名称为user。我使用此print_r(array_keys($args->getEntityChangeSet()))找出正确的字段名称。