Saving metadata for an User using PHP

Discussion in 'Integration' started by hacketingio, Oct 18, 2018.

  1. hacketingio

    hacketingio New Member

    Joined:
    Oct 18, 2018
    Messages:
    2
    So, I have hooked with the event USER_AFTER_INSERT where I did some api calls. Now I need to save some of the meta data for the user which I can grab later on with php. Anyone know how to do that?

    BTW, I am new to this software, still going through the source code.
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    You can create new SQL field with table with users to store this meta data. You can do it at:
    aMember CP -> Configuration -> Add User Field

    and then save your metadata to this field:
    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::USER_AFTER_INSERT, function(Am_Event $e) {

         
    /* @var $user User */
        
    $user $e->getUser();

        
    $user->fieldname 'metadata';
        
    $user->save();

    });
  3. hacketingio

    hacketingio New Member

    Joined:
    Oct 18, 2018
    Messages:
    2
    Thanks for your reply. It worked, although I took slightly different approach. I have hooked into before insert and updated the `$user` object.

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::USER_BEFORE_INSERT, function(Am_Event $e) {
       
      
    // do external api operation   

      
    if (isset($data['foo'])) {
      
    $user->foo $data['foo'];
      }
    });
    running `$user->save()` will trigger user update hooks which I didn't want to do.

    Another Question: In aMember CP -> Configuration -> Add User Field Where does amember stores data when SQL field is not selected?
  4. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You do not need to call "save" in event Am_Event::USER_BEFORE_INSERT, just set field to object.

    In event of you chose [DATA] type of additional field then aMember store it in another table (am_data)
    and you need to set it to record this way:
    PHP:
    $user->data()->set('foo'$data['foo']);

Share This Page