Updating Expressionengine subscriptions

Discussion in 'Integration' started by edusites, Oct 14, 2014.

  1. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    When a new subscription is added or an existing subscription is changed in aMember I need update th exp_members_subscriptions table in ExpressionEngine.

    I have looked through the aMember code but it isn't obvious where I should make changes, can anyone provide guidence please?
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,660
  3. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    that's great, thank you!
  4. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    If I understand correctly; I add functions and hooks for each subscription event inside site.php.

    But how I access the expressionengine database to update the table from this file?
  5. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,660
    Yes, you are right regarding events.

    To update database you can use any standard PHP approach. It is either mysqli (http://php.net/mysqli) or PDO (http://php.net/pdo).

    Another approach is use aMember function (which utilize PDO behind the scene). Please have to look
    http://www.amember.com/docs/API/Db (Please note aMember database user should has access to your Expressionengine database).

    I hope it will be useful for you.
  6. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    I would prefer to use the aMember API, but the documenation seems to assume you want to access the am database, how do I tell the API that I want to use the expressionengine database?
  7. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,660
    In case aMember database user (amember/application/configs/config.php) has access to your Expressionengine database than you can use this code:
    PHP:
    Am_Di::getInstance()->db->query("INSERT INTO exp_members_subscriptions .....");
    Otherwise you need to grant necessary access to amember database user.
  8. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    how does it know to use the expressionengine database for this query?
  9. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,660
    You are right. It is my mistake. This code will work only if you have all tables (aMember and Expressionengine in same database).

    In case you have tables for Expressionengine in separate database you can use:
    PHP:
    $db Am_Db::connect(array(
        
    'user' => 'Username',
        
    'pass' => 'Password',
        
    'host' => 'Host',
        
    'db' => 'Database Name',
        
    'port' => 'Port'
    ));

    $db->query("INSERT INTO exp_members_subscriptions ....."); 
    Last edited: Oct 15, 2014
    edusites likes this.
  10. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    Thank you.

    (it's 'db' not 'path')
  11. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,660
    Array key should be path. There is not mistake.
  12. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,660
    Yes, it is db. I corrected my original replay.
  13. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    I have a new hook working successfully for Am_Event::SUBSCRIPTION_ADDED, but I cannot get hooks for SUBSCRIPTION_REMOVED
    SUBSCRIPTION_UPDATED
    to work.

    I have tried changing these to :
    Am_Event::USER_AFTER_DELETE
    Am_Event::USER_AFTER_UPDATE

    as the documentation suggests http://www.amember.com/docs/API/HookManager (the first 2 are deprecated), but still no luck.
  14. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    Ignore the last post, I had coded it wrong!

Share This Page