has_many problem

edited February 2014 in Modules
I am sure its something basic that I am missing.

I have following models

project_categories_model
projects_model
project_images_model

the idea is that categories have many projects have many images, I am trying to get tree functionality working and link these modules together so I may upload multiple images when defining projects, or add a category when defining a project etc. am I using has_many incorrectly?

//in project_images_model
public $has_many = array('project_id' => array(FUEL_FOLDER => 'project_images_model');

//in projects_model
public $has_many = array('project_id' => array(FUEL_FOLDER => 'project_categories_model');

Comments

  • edited 9:43AM
    A couple questions:

    1. Can a project belong to only one category?
    2. Will the images be associated to only one project?
  • edited 9:43AM
    Project can be part of one Category (n:1) where n=projects
    Image can be associated with one project (n:1) where n=images
  • edited 9:43AM
    In that case, I would use a foreign_key property instead of has_many property:
    // in the projects model public $foreign_keys = array('category_id' => array(FUEL_FOLDER => 'fuel_categories_model'));
    // in the projects model public $foreign_keys = array('project_id' => 'projects_model');
    Although the dropdown for the category in the projects form will automatically be generated, the images will need to actually be manually added in the forms_fields method. You could do something like this:
    ... if (!empty($values['id'])) { $CI =& get_instance(); $CI->load->module_model('project_images_model'); $images = $CI->project_images_model->options_list('id', 'name', array('project_id' => $values['id'])); $str = ''; foreach($images as $key => $image) { $edit_uri = ($CI->fuel->admin->is_inline()) ? 'inline_edit' : 'edit'; $str .= '<a href="'.fuel_url('project_images/'.$edit_uri.'/'.$key).'">' . $image . '</a><br>'; } $create_uri = ($CI->fuel->admin->is_inline()) ? 'inline_create' : 'create'; $str .= '<br><br><a href="'.fuel_url('project_images/'.$create_uri).'" class="btn_field">Add</a><br>'; ...
  • edited 9:43AM
    Thanks for the response.
    I understand now the mapping of foreign_keys however the tree functionality is still not working. It just says no data to display.

    // in projects_model - this populates the categories properly in form and listing but tree view displays no data

    public $foreign_keys = array('project_category_id' => array('category_id' => 'project_categories_model'));
  • edited 9:43AM
    What does your tree method look like?
  • edited 9:43AM
    // in projects model.
    function tree()
    {
    return $this->_tree('foreign_keys');
    }
  • edited 9:43AM
    There is an issue with that method if you were using model name that was different then the table name. A fix was posted yesterday in the develop branch which will be in the next patch release:
    https://github.com/daylightstudio/FUEL-CMS/commit/8504510f772896a87230281c182b452345445e56
  • edited 9:43AM
    Thanks - I updated my copy with the patch and The tree is now showing up
    But its only showing one record under each category.
  • edited 9:43AM
    That's a bug. A fix for that has been pushed to the develop branch:
    https://github.com/daylightstudio/FUEL-CMS/commit/7215165839dc6cb3537850b249af4b59e8c0ca5e
  • edited February 2014
    Perfect - That fixed the problem with the tree display but the record id is wrong.
    It is displaying the category's record id for each record under it, when it should be the actual record id of that record.
  • edited 9:43AM
    Alright... hopefully third times a charm here. I've posted another fix for that so please test it out and let me know if you have any other issues. There was also an issue with the links when the module_uri module parameter was different from the module_name parameter.
    https://github.com/daylightstudio/FUEL-CMS/commit/e700d0abf783fd8be9836ddef99836b0c730a914
  • edited 9:43AM
    Nice it is working fine now. Thanks.
Sign In or Register to comment.