Help with saving data in models [SOLVED]

edited July 2012 in News & Announcements
Hello,

I have 4 models that should interact with each other.

categories
sub_categories
members
categories_to_members

Some categories have sub categories and some not.

When I create a member I select one category for him, but I don't have to select a sub category.

When I do select a sub category it is not being saved to the database.

Can you please tell me what code should I add and where?

My models code:
Categories:
class Categories_model extends Base_module_model { public $record_class = 'Category'; public $parsed_fields = array('content', 'content_formatted'); function __construct() { parent::__construct('categories'); } // cleanup category to members function on_after_delete($where) { $this->delete_related('categories_to_members_model', 'category_id', $where); } // add unique name validation function on_before_validate($values) { if (!empty($values['id'])) { $this->add_validation('name', array(&$this, 'is_editable'), lang('error_val_empty_or_already_exists', lang('form_label_name')), array('name', $values['id'])); } else { $this->add_validation('name', array(&$this, 'is_new'), lang('error_val_empty_or_already_exists', lang('form_label_name')), 'name'); } return $values; } } class Category_model extends Base_module_record { function get_members($subCategory) { $this->_CI->load->model('categories_to_members_model'); $where = array('category_id' => $this->id, 'sub_cat_id' => $subCategory, 'members.published' => 'yes'); $members = $this->_CI->categories_to_members_model->find_all($where); return $members; } }

sub_categories:
class Sub_categories_model extends Base_module_model { public $record_class = 'Sub_category'; public $foreign_keys = array('cat_id' => 'categories_model'); function __construct() { parent::__construct('sub_categories'); } // add unique name validation function on_before_validate($values) { if (!empty($values['id'])) { $this->add_validation('name', array(&$this, 'is_editable'), lang('error_val_empty_or_already_exists', lang('form_label_name')), array('name', $values['id'])); } else { $this->add_validation('name', array(&$this, 'is_new'), lang('error_val_empty_or_already_exists', lang('form_label_name')), 'name'); } return $values; } function form_fields($values = array()) { $fields = parent::form_fields(); $CI =& get_instance(); $CI->load->model('categories_model'); $cat_options = $CI->categories_model->options_list('id', 'name', array('published' => 'yes')); $fields['cat_id'] = array('type' => 'select', 'options' => $cat_options); return $fields; } } class Sub_category_model extends Base_module_record { }

Categories_to_members:
class Categories_to_members_model extends MY_Model { public $record_class = 'Category_to_member'; public $foreign_keys = array('category_id' => 'categories_model', 'sub_cat_id' => 'sub_categories_model', 'member_id' => 'members_model'); public $parsed_fields = array('content', 'content_formatted'); function __construct() { parent::__construct('categories_to_members'); } function _common_query() { $this->db->select('categories_to_members.*, members.title, members.id, members.content, members.address, members.phone, members.site, members.logo, categories.name AS category_name, categories.published'); $this->db->join('members', 'categories_to_members.member_id = members.id', 'left'); $this->db->join('categories', 'categories_to_members.category_id = categories.id', 'left'); // $this->db->join('sub_categories', 'categories_to_members.sub_cat_id = sub_categories.id', 'left'); } } class Category_to_member_model extends Data_record { public $category_name = ''; public $title = ''; function get_excerpt_formatted($char_limit = NULL, $readmore = '') { $this->_CI->load->helper('typography'); $this->_CI->load->helper('text'); $excerpt = $this->content; if (!empty($char_limit)) { // must strip tags to get accruate character count $excerpt = strip_tags($excerpt); $excerpt = character_limiter($excerpt, $char_limit); } $excerpt = auto_typography($excerpt); $excerpt = $this->_parse($excerpt); if (!empty($readmore)) { $excerpt .= ' '.anchor($this->get_url(), $readmore, 'class="readmore"'); } return $excerpt; } }

Members:
class Members_model extends Base_module_model { public $required = array(); public $parsed_fields = array('content', 'content_formatted'); function __construct() { parent::__construct('members'); // table name } function list_items($limit = NULL, $offset = NULL, $col = 'title', $order = 'asc') { $this->db->select('members.id, title, SUBSTRING(content, 1, 50) AS content, date_added, members.published', FALSE); $data = parent::list_items($limit, $offset, $col, $order); return $data; } function tree() { $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('categories_to_members_model'); $return = array(); $categories = $CI->categories_model->find_all(array(), 'id asc'); $categories_to_members = $CI->categories_to_members_model->find_all('', 'categories.name asc'); foreach($categories as $category) { $cat_id = $category->id; $return[] = array('id' => $category->id, 'label' => $category->name, 'parent_id' => 0, 'location' => fuel_url('categories/edit/'.$category->id)); } $i = $cat_id +1; foreach($categories_to_members as $val) { $attributes = ($val->published == 'no') ? array('class' => 'unpublished', 'title' => 'unpublished') : NULL; $return[$i] = array('id' => $i, 'label' => $val->title, 'parent_id' => $val->category_id, 'location' => fuel_url('members/edit/'.$val->member_id), 'attributes' => $attributes); $i++; } return $return; } function form_fields($values = array()) { $fields = parent::form_fields(); $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('sub_categories_model'); $CI->load->model('categories_to_members_model'); $CI->load->model('member_images_model'); $category_options = $CI->categories_model->options_list('id', 'name', array('published' => 'yes'), 'name'); $category_values = (!empty($values['id'])) ? array_keys($CI->categories_to_members_model->find_all_array_assoc('category_id', array('member_id' => $values['id'], 'categories.published' => 'yes'))) : array(); $fields['categories'] = array('label' => 'Categories', 'type' => 'array', 'class' => 'add_edit categories', 'options' => $category_options, 'value' => $category_values, 'mode' => 'multi'); $cat_options = $CI->sub_categories_model->options_list('id', 'name'); $fields['sub_cat_id'] = array('type' => 'select', 'options' => $cat_options); $upload_path = assets_server_path('members/', 'images'); $fields['logo_upload'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE); $fields['image'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE); $fields['add_another_image'] = array('type' => 'button', 'value' => 'Add', 'use_input' => FALSE); return $fields; } }

Comments

  • edited July 2012
    Another problem I have here:
    I have two categories that have no sub categories: Health & Beauty and Outdoors.

    When I click on the menu item: Health & Beauty
    $nav['members/healthbeauty'] = array('label' => Health & Beauty);
    I get this error:
    Fatal error: Call to a member function get_members() on a non-object in C:\wamp\www\clarenssaNewFuel\fuel\application\third_party\fuel\Loader.php(298) : eval()'d code on line 20 Call Stack # Time Memory Function Location 1 0.0009 405544 {main}( ) ..\index.php:0 2 0.0025 469632 require_once( 'C:\wamp\www\clarenssaNewFuel\fuel\codeigniter\core\CodeIgniter.php' ) ..\index.php:236 3 0.1174 3554736 Page_router->_remap( ) ..\CodeIgniter.php:284 4 0.2538 7075104 Page_router->_remap_variables( ) ..\page_router.php:50 5 0.2538 7075288 Fuel_page->variables_render( ) ..\page_router.php:139 6 0.2543 7077176 Fuel_Loader->module_view( ) ..\Fuel_page.php:417 7 0.2543 7077176 Fuel_Loader->view( ) ..\Loader.php:113 8 0.2546 7078056 Fuel_Loader->_ci_load( ) ..\Loader.php:252 9 0.2558 7153736 eval( ''?>uri->segment(2);\r\n$subCategory = $CI->uri->segment(3);\r\n\r\n$CI->load->model(\'images_to_members_model\');\r\n\r\nif(!empty($category)):\r\n\t$CI->load->model(\'categories_model\');\r\n\t\r\n\t// remember, all categories that have a published value of \'no\' will automatically be excluded\r\n\t$category = $CI->categories_model->find_one_by_name($category);\r\n\tif(!empty($subCategory)):\r\n\t$CI->load->model(\'sub_categories_model\');\r\n\t\t$subCategory = $CI->sub_categories_model->find_one_by_url($subCategory);\r\n\t\t$subCatId = $subCategory->id;\r\n\telse:\r\n\t\t$subCatId = 0;\r\n\tendif;\r\n\t\t$members = $category->get_members($subCatId);\r\n\r\nelse:\r\n\t$CI->load->model(\'members_model\');\r\n\r\n\t// remember, all members that have a published value of \'no\' will automatically be excluded\r\n\t$members = $CI->members_model->find_all();\r\nendif; ?>\r\n name; ?> \r\n\' . $subCategory->name . \'\'; ?>\r\n \r\n\t\r\n \r\n\t \r\n id; ?>\r\n\t\r\n\t id, \'Edit: \'.$member->title, \'members\'); ?>title; ?> \r\n\t Address: address; ?> \r\n\t Phone: phone; ?> \r\n\t Web site: site; ?> \r\n\t content; ?> \r\n \r\n \t \r\n \tload->model(\'member_images_model\');\r\n\t\t\t$images = $CI->member_images_model->find_all(array(\'member_id\'=>$memberId), \'id asc\');\r\n\r\n\t\t\tforeach($images as $img):\r\n\t\t\t\t$image = img_path() . \'members/\' .$img->image;\r\n\t\t\t\t\r\n\t\t\t\techo \' \r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t \';\r\n\t\t\tendforeach; ?>\r\n \r\n \r\n \r\n \r\n \r\n\r\n\r\n'' )

    If I click on outdoors I get no error.

    My nav code:
    $nav = array(); $cats = fuel_model('categories', array('find' => 'all', 'where' => array('published' => 'yes'), 'name asc')); $CI->load->model('sub_categories_model'); $CI->load->model('categories_to_members_model'); foreach($cats as $cat): $id = $cat->id; $title = $cat->name; $catUrl = $cat->url; $numOfSubCats = $CI->sub_categories_model->record_count(array('cat_id'=>$id)); $total = $CI->categories_to_members_model->record_count(array('category_id'=>$id)); $label = $title . ' (' . $total . ')'; $nav['members/' . $catUrl] = array('label'=>$label); if($numOfSubCats != 0): $subCats = fuel_model('sub_categories', array('find' => 'all', 'where' => array('cat_id' => $id), 'name asc')); foreach($subCats as $sub): $subCatId = $sub->id; $url = $sub->url; $subName = $sub->name; $subTotal = $CI->categories_to_members_model->record_count(array('category_id'=>$id, 'sub_cat_id'=>$subCatId)); if(empty($subTotal)) $subTotal = 0; $subLabel = $subName . ' (' . $subTotal . ')'; $nav['members/' . $title . '/' . $url] = array('label' => $subLabel, 'parent_id' => 'members/' . $title); endforeach; endif; endforeach;

    Why do I get this error?
  • edited 11:17AM
    Now suddenly I get a new problem. The dropdown menu does not appear.

    A few minutes ago, as I wrote my last post, I had a dropdown menu, now I don't. :(
    Under accommodation I should have a few sub categories, where did they disappear to?

    Dear god, what is going on here?
    <ul id="accordion" class="accordion"> <li class="first"><a class="active" href="http://localhost/clarenssaNewFuel/members/accommodation" title="Accommodation (2)">Accommodation (2)</a></li> <li><a class="active" href="http://localhost/clarenssaNewFuel/members/healthbeauty" title="Health &amp; Beauty (1)">Health &amp; Beauty (1)</a></li> <li><a class="active" href="http://localhost/clarenssaNewFuel/members/outdoors" title="Outdoors (1)">Outdoors (1)</a></li> <li class="last"><a class="active" href="http://localhost/clarenssaNewFuel/members/food" title="Food (0)">Food (0)</a></li> </ul>
  • edited 11:17AM
    Another problem I have, when I edit a member, it creates a new record in my members table instead of just editing the existing one.
  • edited 11:17AM
    Ok, found the problem with the dropdown.
    I have added a new field for the url so it won't use the & sign in it.
    Didn't change it in the sub menu parent item.

    This problem is fixed.

    But the rest of the problems are causing me to lose hair.
  • edited 11:17AM
    Ok, found another solution for the health & beauty problem.

    Again, the url is healthbeauty but the category name is Health & Beauty, so this line is wrong:

    $category = $CI->categories_model->find_one_by_name($category);

    Changed it to:
    $category = $CI->categories_model->find_one_by_url($category);

    Thank you god for the help :)

    Now it is only the sub categories not being saved to the database, and editing a member creates a new record in the members table.

    If anyone can help.
  • edited 11:17AM
    And the colorbox problem
  • edited 11:17AM
    Members, categories.
    When saving a member, in an on_after_save hook in your members model check your normalized_saved_data array (check I have that name correct) for category data.

    If there is some, save it appropriately.


    get_members() error.
    The formatting of that line's a pit of a night mare but I think get_members() is only called here right?

    $members = $category->get_members($subCatId);

    Is category a data record instance?
  • edited 11:17AM
    Hi Lance,

    Thanks for the help but I still don't understand what do I need to change.

    I looked at the userguide Creating Simple Modules tutorial and copied the code from there, although I have to admit, I don't fully understand what it is doing.

    The sub_cat_id should be saved in the categories_to_members table.
    The tutorial code:
    function on_after_save($values) { $data = (!empty($this->normalized_save_data['categories'])) ? $this->normalized_save_data['categories'] : array(); $this->save_related('categories_to_articles_model', array('article_id' => $values['id']), array('category_id' => $data)); }

    My on_after_save hook in the members model:
    function on_after_save($values) { $catId = (!empty($this->normalized_save_data['categories'])) ? $this->normalized_save_data['categories'] : array(); $subCatId = (!empty($this->normalized_save_data['sub_categories'])) ? $this->normalized_save_data['sub_categories'] : array(); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $catId), array('sub_cat_id' => $subCatId));

    I think I should not use save_related for the sub_category since it is not many to many, but what should I use?

    If you can help with that, that's great since I want to understand it.

    Now, I changed the catgories from multiple to one, so it is a select list, just like the sub cateogry.

    Now no record is being created in the categories_to_members table and lots of empty records are being created in the members table.

    My members_model on_after_save after my change:
    function on_after_save($values) { $catId = $values['cat_id']; $subCatId = $values['sub_cat_id']; $this->save('categories_to_members_model', array('member_id' => $values['id'], 'category_id' => $catId, 'sub_cat_id' => $subCatId), TRUE, TRUE);}

    I guess you can see, I have no clue what I'm doing, and I've searched tutorials, userguides and what not, can't understand what do I need to do.
  • edited 11:17AM
    Another try I made is that both are multiple choices.

    Tried that:
    function on_after_save($values) { $catId = (!empty($this->normalized_save_data['categories'])) ? $this->normalized_save_data['categories'] : array(); $subCatId = (!empty($this->normalized_save_data['sub_categories'])) ? $this->normalized_save_data['sub_categories'] : array(); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $catId, 'sub_cat_id'=>$subCatId));

    Only the category id was saved.

    Tried that:
    function on_after_save($values) { $catId = (!empty($this->normalized_save_data['categories'])) ? $this->normalized_save_data['categories'] : array(); $subCatId = (!empty($this->normalized_save_data['sub_categories'])) ? $this->normalized_save_data['sub_categories'] : array(); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $catId)); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('sub_cat_id'=>$subCatId));
    Only the sub_category was saved.

    So, if you can please help me and give me the code I need to put for saving a value from a dropdown list, and the code for saving more than one multiple value.

    Thank you very much.
  • edited July 2012
    Your post from 9:35 (directly under mine) looks like you're calling the save_related function incorrectly.

    It's signature is:
    function save_related($model, $key_field, $data)

    This:
    $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $catId), array('sub_cat_id' => $subCatId));

    Should be something like (last array):
    $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $catId, 'sub_cat_id' => $subCatId));

    The changes to your on_after_save are using $this->save() incorrecly. Or is that a typo and should be save_related?

    Your last post, 9:53 (directly above).
    Code here looks good. Can you print out the post data for those two fields? I'm assuming the array structure/format is causing some problem.

    It's probably a good idea to see what the save_related method is doing, it's pretty short, head to /application/core/MY_Model.php about line 1115
  • edited 11:17AM
    Hi Lance,

    I have tried 2 different scenarious with no success.

    First I tried to have category with multiple select and sub category with a dropdown where only one value can be selected. That's where I tried to use $this->save instead of $this->save_related

    That didn't work.

    Second I changed the sub category to be exactly like the category, multiple values can be selected.

    Tried that:
    $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $catId)); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('sub_cat_id' => $subCatId));
    Only the sub category was saved.

    Tried that:
    $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $catId, 'sub_cat_id'=>$subCatId));
    Only the category was saved.

    When you say:
    Can you print out the post data for those two fields? I'm assuming the array structure/format is causing some problem.

    How do I do that? I tried to add this print_r($values); in the on_after_save but after save goes strait to edit so nothing displays on the screen.

    I tried to put it in the form_fields function, nothing displays.

    I tried to put
    print_r($key_field); print_r($data);
    in the My_model save_related function, nothing displays.

    How can I display the post values?

    Where can I print the post data so I will see it?

    So now, at the end, I have a select list for the category field that looks like this:
    <select name="category" id="category"> <option value="1" label="Accommodation">Accommodation</option> <option value="4" label="Food">Food</option> <option value="2" label="Health &amp; Beauty">Health &amp; Beauty</option> <option value="6" label="Hungry or Thirsty?">Hungry or Thirsty?</option> <option value="3" label="Outdoors">Outdoors</option> <option value="5" label="Retail">Retail</option> </select>
    And a multiple select for the sub category that looks like this:
    <select name="sub_categories[]" id="sub_categories" class="add_edit sub_categories" multiple="multiple"> <option value="1" label="B &amp; B">B &amp; B</option> <option value="2" label="Guesthouse">Guesthouse</option> <option value="4" label="Hotel">Hotel</option> <option value="3" label="Self-Catering">Self-Catering</option> </select>

    In my members model I have this:
    function form_fields($values = array()) { $fields = parent::form_fields(); $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('sub_categories_model'); $CI->load->model('categories_to_members_model'); $CI->load->model('member_images_model'); $catOptions = $CI->categories_model->options_list('id', 'name'); $fields['category'] = array('type'=>'select', 'options'=>$catOptions); $subCategoryOptions = $CI->sub_categories_model->options_list('id', 'name', array(),'name'); $subCategoryValues = (!empty($values['id'])) ? array_keys($CI->categories_to_members_model->find_all_array_assoc('sub_cat_id', array('member_id' => $values['id']))) : array(); $fields['sub_categories'] = array('label' => 'Sub_Categories', 'type' => 'array', 'class' => 'add_edit sub_categories', 'options'=>$subCategoryOptions, 'value' =>$subCategoryValues, 'mode'=>'multi'); } function on_after_save($values) { $catId = $values['cat_id']; $subCatId = (!empty($this->normalized_save_data['sub_categories'])) ? $this->normalized_save_data['sub_categories'] : array(); $this->save_related('categories_to_members_model', array('member_id'=> $values['id']), array('category_id'=>$catId, 'sub_cat_id'=>$subCatId)); }

    The result, no record created in the cateogries_to_members table.
    The index on the categories_to_members table is:
    category_id
    sub_cat_id
    member_id

    I tried this as well, doesn't work, no record created.
    $this->save_related('categories_to_members_model', array('category_id' => $catId, 'sub_cat_id' => $subCatId, 'member_id' => $values['id']), array('category_id' => $catId, 'sub_cat_id' => $subCatId));

    I checked the save_related function on My_model but it didn't help me understand why my code is not working.

    Thanks.
  • edited July 2012
    do a:

    print_obj($_POST);die;

    in your on_after_save() method.

    The categories/sub categories information would have been cleaned out of $values by this stage so you won't see them there. That's why your using the $this->normalized_save_data array.
  • edited 11:17AM
    Thank you, there is the array:
    Array ( [title] => test9 [address] => [phone] => [cellphone] => [site] => [logo] => [content] => [slug] => [category] => 1 [sub_categories] => Array ( [0] => 1 )
  • edited 11:17AM
    Another debug:
    I added these lines in the save_related function in My_model:
    echo "Model: $model<br />"; echo "key_field: "; print_r($key_field); echo "<br />data: "; print_r($data);die;
    This is what I get and nothing is being created in the categories_to_members table:
    Model: categories_to_members_model key_field: Array ( [member_id] => 27 ) data: Array ( [category_id] => 1 [sub_cat_id] => 1 )

    Does it maybe have something to do with the categories_to_members model?
    The model code:
    public $record_class = 'Category_to_Member'; public $foreign_keys = array('category_id' => 'categories_model', 'sub_cat_id' => 'sub_categories_model', 'member_id' => 'members_model'); function __construct() { parent::__construct('categories_to_members'); } function _common_query() { $this->db->select('categories_to_members.*, categories.name AS category_name, sub_categories.id AS sub_cat_id, members.id AS member_id, categories.published'); $this->db->join('sub_categories', 'categories_to_members.sub_cat_id = sub_categories.id', 'left'); $this->db->join('categories', 'categories_to_members.category_id = categories.id', 'left'); $this->db->join('members', 'categories_to_members.member_id = members.id', 'left'); }

    Please can someone help? I have to get on with this web site. I am losing my mind.
  • edited 11:17AM
    The save_related method was added to make it easier to save that relationship data. However, all it is really doing is deleting records from the relational table that belong to that member and then looping through the post values with a foreach and saving the new ones to the relational table. Perhaps take that approach in the on_after_save hook instead of using the save_related method.
  • edited 11:17AM
    Ok, trying to add just the category now.

    I have this code:
    class Members_model extends Base_module_model { public $required = array(); public $foreign_keys = array('category_id' => 'categories_model'); ...} function on_after_save($values) { $CI =& get_instance(); $CI->load->model('categories_to_members_model'); $data = (!empty($this->normalized_save_data['category_id'])) ? $this->normalized_save_data['category_id'] : array(); $CI->categories_to_members_model->save($data, TRUE, TRUE); }
    Nothing.

    Today I started it all from the beginning, I copied all the articles, categories and categories_to_articles from the userguide.

    Changed it all to my tables.

    Changed the multiple categories to a foreign key.

    Worked well.

    Added sub categories, broke everything. Tried again, cannot make it work again.
  • edited 11:17AM
    This is working:
    members_model:
    function form_fields($values = array()) { $fields = parent::form_fields(); $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('categories_to_members_model'); $category_options = $CI->categories_model->options_list('id', 'name', array('published' => 'yes'), 'name'); $category_values = (!empty($values['id'])) ? array_keys($CI->categories_to_members_model->find_all_array_assoc('category_id', array('member_id' => $values['id'], 'categories.published' => 'yes'))) : array(); $fields['categories'] = array('label' => 'Categories', 'type' => 'array', 'class' => 'add_edit categories', 'options' => $category_options, 'value' => $category_values, 'mode' => 'multi'); return $fields; } // add categories function on_after_save($values) { $data = (!empty($this->normalized_save_data['categories'])) ? $this->normalized_save_data['categories'] : array(); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $data)); }

    Now how do I change it to work with a dropdown for the category and a multiple chioce for the sub_category and save it to the categories_to_members table?
  • edited 11:17AM
    At last I managed to solve this problem.
    I found this post which helped:
    http://www.getfuelcms.com/forums/discussion/584/save-related-data-to-another-table/p1

    function form_fields($values = array()) { $fields = parent::form_fields(); $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('sub_categories_model'); $CI->load->model('categories_to_members_model'); $category_options = $CI->categories_model->options_list('id', 'name', array('published' => 'yes'), 'name'); $category_values = (!empty($values['id'])) ? array_keys($CI->categories_to_members_model->find_all_array_assoc('category_id', array('member_id' => $values['id'], 'categories.published' => 'yes'))) : array(); $fields['category'] = array('label' => 'Categories', 'type' => 'select', 'class' => 'add_edit categories', 'options' => $category_options, 'value' => $category_values); $subCategoryOptions = $CI->sub_categories_model->options_list('id', 'name', array(), 'name'); $subCategoryValues = (!empty($values['id'])) ? array_keys($CI->categories_to_members_model->find_all_array_assoc('sub_cat_id', array('member_id' => $values['id'], 'categories.published' => 'yes'))) : array(); $fields['sub_categories'] = array('label' => 'Sub Categories', 'type' => 'array', 'class' => 'add_edit categories', 'options' => $subCategoryOptions, 'value' => $subCategoryValues, 'mode' => 'multi'); return $fields; } // add categories function on_after_save($values) { $this->load->model('categories_to_members_model'); $catId = $_POST['category']; if(!empty($_POST['sub_categories'])): foreach($_POST['sub_categories'] as $sub): $p = $this->categories_to_members_model->create(); $p->member_id = $values['id']; $p->category_id = $catId; $p->sub_cat_id = $sub['id']; $p->save(); endforeach; else: $p = $this->categories_to_members_model->create(); $p->member_id = $values['id']; $p->category_id = $catId; $p->sub_cat_id = 0; $p->save(); endif; return $values; }

    Now everything is working 100%. Halleluiah!

    Don't know what went wrong with save_related, but at least everything is working now.
Sign In or Register to comment.