Detecting change, using getUser() and getOldUser()

Discussion in 'Customization & add-ons' started by pagescreative, Aug 21, 2020.

  1. pagescreative

    pagescreative New Member

    Joined:
    Jun 24, 2020
    Messages:
    2
    Hi, I'm using an `onUserBeforeUpdate()` hook to fire some user detail at an external API.

    I want to update the 3rd-party ONLY if items have changed. At the moment the items I'm checking are Firstname, Lastname and Email.

    Is there a flag or setting in the $event or $user that states whether the details have been updated?

    Right now I'm doing something like this:
    Code:
    $user = $event->getUser();
    $old = $event->getOldUser();
    
    $oldparams = [
        'oldUserEmail' => $old->email,
        'oldUserFirst' => $old->name_f,
        'oldUserLast' => $old->name_l
    ];
    $newparams = [
        'newUserEmail' => $user->email,
        'newUserFirst' => $user->name_f,
        'newUserLast' => $user->name_l
    ];
    
    $changes = array_diff((array) $old, (array) $user);
    If there was some sort of 'contains update' flag, I could avoid unnecessary checks on these arrays.

    Thanks! Ben
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I believe the best approach is just compare these fields ie.:
    PHP:
    if (
        
    $old->email != $user->email
        
    || $old->name_f != $user->name_f
        
    || $old->name_l != $user->name_l
    ) {
        
    //do your actions
    }

Share This Page