Update user field using hook in site.php

Discussion in 'aMember Pro v.4' started by chriswillow, Oct 18, 2012.

  1. chriswillow

    chriswillow New Member

    Joined:
    Sep 20, 2009
    Messages:
    4
    I'd like to modify a field in the user table every time a subscription payment is made.
    Here's what I have so far (got this code off the forum as well):
    PHP:
    Am_Di::getInstance()->hook->add('paymentAfterInsert''customPaymentAfterInsert');
    function 
    customPaymentAfterInsert(Am_Event_PaymentAfterInsert $event)
    {
        
    $invoice $event->getInvoice();
        $
    $user_id $invoice->user_id;
        
    //update db where user_id=$user_id with some value
    }
    My question is how can I execute the query using amember's active record?

    Thanks in advance!
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    It is really easy
    PHP:
    Am_Di::getInstance()->hook->add('paymentAfterInsert''customPaymentAfterInsert');
    function 
    customPaymentAfterInsert(Am_Event_PaymentAfterInsert $event)
    {
           
    $payment $event->getPayment(); // InvoicePayment object
           
    $invoice $event->getInvoice();  // Invoice object
           
    $user $invoice->getUser(); // User object
           // now change field (only created as SQL field!)
           
    $user->customfield 'newvalue';
           
    $user->update();
           
    // to run any custom query
           
    Am_Di::getInstance()->db->query("UPDATE xx SET yy=22"); 
           
    // or run another custom select
          
    $rows Am_Di::getInstance()->db->select("SELECT * FROM xx WHERE user_id=?d"$user->user_id);
    }
     
  3. chriswillow

    chriswillow New Member

    Joined:
    Sep 20, 2009
    Messages:
    4
    Perfect, just what I needed. Will save this for future reference.
    Thanks:)
  4. miso

    miso aMember Pro Customer

    Joined:
    Aug 22, 2006
    Messages:
    543
    how would you write that hook/query to update a plain_password custom field with the user's actual password every time a new user registers or updates their password or the admin updates the password manually?
  5. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Here is example:
    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::SET_PASSWORD'onSetPassword');
    function 
    onSetPassword(Am_Event_SetPassword $e){
        
    $user $e->getUser();
        
    $password $e->getPassword();
        
    $user->updateQuick('plain_password'$password); // Or $user->data()->set('plain_password', $password)->update(); if field is not sql
    }

Share This Page