Understanding Fuel Module Hooks

edited October 2012 in Modules
I have a 'clients' table I want to hook into the fuel_users table by using an 'after_create_user' module hook. There is limited information on this so I bet answering this will be useful for someone else as well.

The clients table has the following fields: id, fuel_user_id, company_name and contact_phone. I'll be adding clients to the Fuel CMS users one at a time manually. When that happens I want to automatically create a record in the clients table saving the id from fuel_users into the fuel_users_id of the clients table. I'll then go into the clients module and enter the company name and phone.

In /fuel/application/config/hooks.php I have the following:

$hook['after_create_user'] = array( 'class' => 'Clients', 'function' => 'create_client', 'filename' => 'clients_model.php', 'filepath' => 'models' ); // include hooks specific to FUEL include(FUEL_PATH.'config/fuel_hooks.php');
In /fuel/application/models/clients_model.php I have the following function:

function create_client($data) { $CI =& get_instance(); $values['fuel_user_id'] = $data['id']; $this->db->insert($values); }
I'm sure I've got the module hook in the wrong place or whatever. When I create a new fuel user in the CMS nothing happens. No errors are thrown but it's obviously not seeing the hook.

What am I doing wrong?

Thanks.

Comments

  • edited 5:41PM
    You're gonna love this... It's the userS module:

    $hook['after_create_users'] = array(...)
  • edited 5:41PM
    Doh! Thanks. I hate it and love it at the same time. Getting an error now though. As a reminder, this is the function in clients_model.php.

    function create_client($data) { $CI =& get_instance(); $db['default']['db_debug'] = false; $values['fuel_user_id'] = $data['id']; $this->db->insert($values); }
    The error I'm getting is:

    You must use the "set" method to update an entry.

    It's referring to the line:
    $this->db->insert($values);

    I'm trying to insert a new record into the clients table, not update an existing record. I checked the clients table and the only field that's not null is fuel_user_id which is the one field I'm saving the new fuel_users id value to. Stumped again.
  • edited 5:41PM
    I think the issue is because you need to specify the table name as the first parameter before specifying the $values since you are using the CI active record insert. If you use the model's built in insert method, it already makes that assumption:
    function create_client($data) { $CI =& get_instance(); $db['default']['db_debug'] = false; $values['fuel_user_id'] = $data['id']; $this->db->insert('my_table', $values); }
    OR
    function create_client($data) { $CI =& get_instance(); $db['default']['db_debug'] = false; $CI->load->model('my_model'); $CI->my_model->insert($values); }
  • edited 5:41PM
    That was it! Thanks. I thought the table was automatically assigned inside the Clients_model class. Much appreciated.
  • edited October 2012
    (The following question is similar to this one but I thought it might be best to place this here since there is a slight difference and it directly relates to this thread. )

    Is there a way to conditionally hide the item_actions buttons (save, delete, duplicate, etc) from anyone who is not a super admin in a specific module?
    It seemed logical to do that in MY_fuel_modules.php where I'd do something like:
    $CI =& get_instance(); $user = $CI->fuel_auth->user_data(); if(($user['super_admin']== 'no')) { $clients_array = array( 'module_name'=>'Client Info', 'item_actions' => array('save', 'view') ); } else { $clients_array = array( 'module_name'=>'Client Info' ); } $config['modules']['clients'] = $clients_array;
    But that doesn't work as it seems the $CI instance is not accessible from there. Is there a way to set these values conditionally in my clients_module.php file? I saw where I could include the following as an override for the get_others method and it does work, but I want that to be done conditionally as well:
    function get_others($display_field, $id, $val_field = NULL) { return array(); }
  • edited 5:41PM
    Try adding a function_exists('get_instance') around the logic in MY_fuel_modules.php. That file also gets included very early on to create routes and before the $CI object is created.
  • edited 5:41PM
    Thanks for the reply. Just now getting back to this. So, are you saying to place this in MY_fuel_modules.php?
    if(function_exists('get_instance')) { $CI =& get_instance(); $user = $CI->fuel_auth->user_data(); if(($user['super_admin']== 'no')) { $clients_array = array( 'module_name'=>'Client Info', 'item_actions' => array('save', 'view') ); } else { $clients_array = array( 'module_name'=>'Client Info' ); } $config['modules']['clients'] = $clients_array; } else { $config['modules']['clients'] = array( 'module_name'=>'Client Info', 'item_actions' => array('save', 'view'), 'table_actions' => array('edit', 'view') ); }
    The above doesn't work. I get an empty page. Here's what is in the php error log.
    [01-Nov-2012 10:02:24] PHP Fatal error: Call to a member function view() on a non-object in /Applications/MAMP/htdocs/mpg/fuel/application/views/_blocks/header.php on line 40Any ideas?
  • edited 5:41PM
    Could that error be because you are using "$this" instead of "$CI" in your _blocks/header.php file?
  • edited 5:41PM
    That was the case. I was loading the main_nav as well as the footer_nav blocks like this:
    <?php $this->load->view('_blocks/main_nav'); ?>
    Changed them to:
    <?php $CI->load->view('_blocks/main_nav'); ?>There's no error in the php err log and I can navigate the site just fine but when I nav to /fuel to log in to the admin it jumps straight to /fuel/dashboard (even if I'm logged out).

    If blocks have to be loaded using $CI in this case could that also be affecting the fuel module? For example, the fule block - fuel_header.php loads a view like this:
    <?=$this->load->module_view(FUEL_FOLDER, '_blocks/fuel_header_jqx', array(), TRUE)?>
  • edited 5:41PM
    I don't think that would be affecting the fuel module. The top 2 load view files from the applications folder and immediately output the contents. The second loads the block view file from the fuel modules, passing it an empty array of variables and the TRUE returns it as a string; but since it's using the short syntax (<?=?>) it echoes it out.

    If you are still having troubles logging out, try killing the browser cookie and see if you can replicate the problem.
Sign In or Register to comment.