Editing a user in custom module

edited June 2015 in Share
I have a custom model for users to access the module I have written. The SQL consists of an id, fuel_user_id, fuel_email, company_id, and active.

In my list_items, I do a select and a join for the fuel_user_id and another one the companiy_id. This all seems to work. The Users list properly with the fuel name and last name concatenated, the fuel email and the company name. And, my form shows up fine for editing. But, the breadcrumb trail shows "Users > yes" where "yes" is coming from the "Active" (published). How can I fix this?

Also, when editing the form (does not show in create), there is a dropdown where all the buttons are (Save, view, deactivate, etc) that says "Select another...". If I click this, the data shows like this:

Select another...
John Doe
The Company Name

What am I doing wrong here?

Also, how can I tell if the form is a create or edit? I added code so that users are not already displayed if they are in the system (for create), but when I edit mode, they go missing, so you cannot edit someone who is in the system. Currently, I have the code commented out. I need some conditional on it that I can't figure out. Should I parse the uri and look for "create" vs. "edit" or is there a fuel variable that I couldn't find?


  • edited 3:21AM
    Regarding the "yes" column, I would take a guess that the Active column is the 2nd column defined in the table. If so you could configure the module's default column with the "default_col" or possibly the "display_field" keys - see "additional configuration parameters" in the simple module docs page
  • edited 3:21AM
    The name I want to show up as the default column (or display_column) is bound by this statement in the sql:

    CONCAT(fuel_users.first_name, " ", fuel_users.last_name) as name

    but, if I use this: 'display_field' => 'name',

    then the sql is changed to "ORDER BY `navtech_usert`.`name`" which throws an SQL error.

    Is there any way around this?
  • edited June 2015
    Yes. I would add the following to the _common_query method:
    function _common_query() { parent::_common_query(); $this->db->select('CONCAT(fuel_users.first_name, " ", fuel_users.last_name) as name', FALSE); }

    Then you will need to overwrite your options_list method:
    public function options_list($key = 'fuel_users.id', $val = 'name', $where = array(), $order = TRUE, $group = TRUE) { if (empty($val) OR $val == 'name') { $val = 'CONCAT(fuel_users.first_name, " ", fuel_users.last_name) as name'; $order = 'fuel_users.first_name asc'; } return parent::options_list($key, $val, $where, $order); }

    Additionally, if you have any joins, in 1.3 a _common_joins method can be implemented. The _common_joins will be run in the options_list, list_items and any find_ model methods (e.g. find_one, find_by_key, find_all)
  • edited June 2015
    I did all this, but the resulting sql on the select part of the statement is still being appended with navtech_users.display_name (I changed it from name to display_name). display_name is one of the columns being returned, but somewhere it is being appended to and the order by:

    SELECT CONCAT(fuel_users.first_name, " ", fuel_users.last_name) as display_name, navtech_users.id, fuel_users.email as email, navtech_companies.name as company, navtech_users.active, fuel_users.active as factive, navtech_users.fuel_user_id, navtech_users.display_name FROM (`navtech_users`) LEFT JOIN `fuel_users` ON `fuel_users`.`id` = `navtech_users`.`fuel_user_id` LEFT JOIN `navtech_companies` ON `navtech_companies`.`id` = `navtech_users`.`company_id` ORDER BY `navtech_users`.`display_name` asc
  • edited 3:21AM
    Since display_name is a derived value, you will need to specify the same thing for the module's default_col parameter:
    'default_col' => 'CONCAT(fuel_users.first_name, " ", fuel_users.last_name)',

    Or simply change it to the first_name
    'default_col' => 'first_name',

    Regarding knowing if it's create or edit, you can test for the value of the id in the form_fields method:
    function form_fields($values) { $fields = parent::form_fields($values); if (!empty($values['id'])) { // only editing stuff here } else { // only create stuff here } }

    Also, the example I had above for the options_list had some incorrect default values (due to copying and pasting). I've adjusted them above so that the $key and $val default method parameters should match more of what you are trying to do.
Sign In or Register to comment.