Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CodeIgniter Ion Auth - set_hooks example
//this would probably be in a library or model
class activity
{
function add($type)
{
$this->db->insert('activity', array(
'type' => $type,
'datetime' => 'now()',
));
}
}
//set the hook
$this->ion_auth->set_hook('post_register', 'add_meta', 'activity', 'add', array('user_added'));
//other stuff...
//after this is called the hook will be executed automatically
$this->ion_auth->register(...
@withremote

This comment has been minimized.

Show comment
Hide comment
@withremote

withremote May 31, 2012

I keep getting the " Using $this when not in object context" error when trying to call the set_hook from my controller.
say i have something like this:

In Controller

$id = 1;
$update = array('email'=>'someone@nowhere.com','api_key'=>'1234test');
// set hook for update meta data
$this->ion_auth->set_hook('post_update_user', 'update_user_meta', 'users_model', 'update_meta', array($id,$update));                    
$this->ion_auth->update($id,$update);

Model

class Users_model extends CI_Model {

protected $meta_cols = array();
protected $tables = array();

function __construct() {
    parent::__construct();
    $this->meta_cols = $this->db->list_fields('meta');
    $this->tables = $this->db->list_tables();
    }

      function update_meta($user_id, array $data)
      {
          $update = array();
          if(is_array($data))
          {
             foreach($this->meta_cols as $cols)
             {
                 if(array_key_exists($cols, $data))
                 {
                     $update[$cols] = $data[$cols];
                 }
             }
          }

          return count($update) > 0 ? $this->db->where($this->tables['meta'].'.user_id',$user_id)->update($this->tables['meta'],$update) : FALSE;

      }

}

The script chokes on the "foreach($this->meta_cols as $cols)" line when called with the set_hook function.

withremote commented May 31, 2012

I keep getting the " Using $this when not in object context" error when trying to call the set_hook from my controller.
say i have something like this:

In Controller

$id = 1;
$update = array('email'=>'someone@nowhere.com','api_key'=>'1234test');
// set hook for update meta data
$this->ion_auth->set_hook('post_update_user', 'update_user_meta', 'users_model', 'update_meta', array($id,$update));                    
$this->ion_auth->update($id,$update);

Model

class Users_model extends CI_Model {

protected $meta_cols = array();
protected $tables = array();

function __construct() {
    parent::__construct();
    $this->meta_cols = $this->db->list_fields('meta');
    $this->tables = $this->db->list_tables();
    }

      function update_meta($user_id, array $data)
      {
          $update = array();
          if(is_array($data))
          {
             foreach($this->meta_cols as $cols)
             {
                 if(array_key_exists($cols, $data))
                 {
                     $update[$cols] = $data[$cols];
                 }
             }
          }

          return count($update) > 0 ? $this->db->where($this->tables['meta'].'.user_id',$user_id)->update($this->tables['meta'],$update) : FALSE;

      }

}

The script chokes on the "foreach($this->meta_cols as $cols)" line when called with the set_hook function.

@benedmunds

This comment has been minimized.

Show comment
Hide comment
@benedmunds

benedmunds Jun 4, 2012

Change your controller to something like this, note the way the class is passed:

$id = 1;
$update = array('email'=>'someone@nowhere.com','api_key'=>'1234test');
// set hook for update meta data
$this->ion_auth->set_hook('post_update_user', 'update_user_meta', $this->users_model, 'update_meta', array($id,$update));                    
$this->ion_auth->update($id,$update);
Owner

benedmunds commented Jun 4, 2012

Change your controller to something like this, note the way the class is passed:

$id = 1;
$update = array('email'=>'someone@nowhere.com','api_key'=>'1234test');
// set hook for update meta data
$this->ion_auth->set_hook('post_update_user', 'update_user_meta', $this->users_model, 'update_meta', array($id,$update));                    
$this->ion_auth->update($id,$update);
@withremote

This comment has been minimized.

Show comment
Hide comment
@withremote

withremote commented Jun 4, 2012

thanks!

@brandonkboswell

This comment has been minimized.

Show comment
Hide comment
@brandonkboswell

brandonkboswell Aug 24, 2012

Is there a way to pass data into the event? I'm trying to create a hook for "post_account_creation_successful" for updating a MailChimp Mailing List, but I'm not sure how to obtain the user information from the event to pipe that to the MailChimp API.

brandonkboswell commented Aug 24, 2012

Is there a way to pass data into the event? I'm trying to create a hook for "post_account_creation_successful" for updating a MailChimp Mailing List, but I'm not sure how to obtain the user information from the event to pipe that to the MailChimp API.

@rafidcse

This comment has been minimized.

Show comment
Hide comment
@rafidcse

rafidcse commented Apr 16, 2014

thanks

@digitaloutback

This comment has been minimized.

Show comment
Hide comment
@digitaloutback

digitaloutback Nov 11, 2014

Can I have the hooked method in one controller and set_hook() run from another controller?

digitaloutback commented Nov 11, 2014

Can I have the hooked method in one controller and set_hook() run from another controller?

@babyfaceEasy

This comment has been minimized.

Show comment
Hide comment
@babyfaceEasy

babyfaceEasy Jan 27, 2016

Is there a way for me to pass data in the present ion-auth function into my own function? i want to hash both the activation code and the id found in the link sent to user's email addresses, in order to activate their account. Then inside the activate (inside ion-auth-model) i want to 'un-hash' it so that ion-auth flow can continue as normal..

babyfaceEasy commented Jan 27, 2016

Is there a way for me to pass data in the present ion-auth function into my own function? i want to hash both the activation code and the id found in the link sent to user's email addresses, in order to activate their account. Then inside the activate (inside ion-auth-model) i want to 'un-hash' it so that ion-auth flow can continue as normal..

@datamweb

This comment has been minimized.

Show comment
Hide comment
@datamweb

datamweb Oct 12, 2017

Hello,@benedmunds
I can run your example. The data in the table is entered correctly.by:
$this->ion_auth->set_hook('post_register', 'add_meta', 'activity', 'add', array('user_added'));
But why does not the following command answer?
$this->ion_auth->set_hook('post_activate', 'add_meta', 'activity', 'add', array('user_added'));
And i reviewed the following. The correct operation is not done.

$this->ion_auth->set_hook(array('post_activate', 'post_activate_successful'), 'add_meta',  'activity', 'add', array('user_added'));
$this->ion_auth->set_hook('post_activate_successful', 'add_meta',  'activity', 'add', array('user_added'));

please guide me?

datamweb commented Oct 12, 2017

Hello,@benedmunds
I can run your example. The data in the table is entered correctly.by:
$this->ion_auth->set_hook('post_register', 'add_meta', 'activity', 'add', array('user_added'));
But why does not the following command answer?
$this->ion_auth->set_hook('post_activate', 'add_meta', 'activity', 'add', array('user_added'));
And i reviewed the following. The correct operation is not done.

$this->ion_auth->set_hook(array('post_activate', 'post_activate_successful'), 'add_meta',  'activity', 'add', array('user_added'));
$this->ion_auth->set_hook('post_activate_successful', 'add_meta',  'activity', 'add', array('user_added'));

please guide me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment