Related Fields not supported in Advanced Modules

edited July 2011 in Bug Reports
Try this, create the sample categories_to_articles module in advanced modules, you will not be able to define a module for the related field ('categories' in the example), this might be because the framework is looking for the module in the main model folder and not your module's model folder.

I had to edit My_Model.php to get this working, I edited the function form_fields at line 1772 and line ~1778, and function initialize at line 115. This way, when defining related fields, I can write 'mymodule/categories' to tell the framework to load categories_model from your module's model directory. I'd share my code but it feels more like a quickfix than a solid solution.

Note: try fixing function form_fields first then testing before fixing function initialize, the $params variable in initialize is populated with a string, I don't know if this is correct behavior or a side-effect of my fix in line 1772 but I fixed it by adding another condition in line 115: is_array($params), hence I deem my solution a quickfix.

Comments

  • edited 7:32PM
    correction for first paragraph: ...you will not be able to define a module for the related field ('categories' in the example), and so your model will not load, this might be...
  • edited 7:32PM
    Would you mind posting the fix you have?
  • edited 7:32PM
    File fuel/application/core/MY_Model.php



    public function initialize, line 112:
    if (!empty($params) && is_array($params))



    public function form_fields, line ~1764 to 1794:
    // create related
    if (!empty($related))
    {
    $key_field = $this->key_field();
    if (is_string($key_field))
    {
    foreach($related as $key => $val)
    {
    #start fix
    $related_model_module = explode('/', $key);
    if(count($related_model_module)>1)
    {
    $module = $related_model_module[0];
    $key = $related_model_module[1];
    $uri = $CI->config->config['modules'][$key]['module_uri'];
    $related_model = $this->load_model(array($module => $key.'_model'));
    }
    else
    {
    $module = '';
    $related_model = $this->load_model($key.'_model'); #was: original line only
    }
    #end fix
    $lookup_model = $this->load_model($val);

    $options = $CI->$related_model->options_list();
    $field_values = (!empty($values['id'])) ? array_keys($CI->$lookup_model->find_all_array_assoc($CI->$related_model->short_name(TRUE, TRUE).'_id', array($this->short_name(TRUE, TRUE).'_id' => $values[$key_field]))) : array();
    #another fix here
    $fields[$key] = array('label' => ucfirst($key), 'type' => 'array', 'class' => 'add_edit '.((isset($uri)) ? $uri : $key), 'options' => $options, 'value' => $field_values, 'mode' => 'multi');
    }
    }
    }
  • edited 7:32PM
    Can try changing lines 1764 to line 1781 in MY_Model to the following? For the key value use a slash syntax like so 'my_module/categories' :
    if (!empty($related)) { $key_field = $this->key_field(); if (is_string($key_field)) { foreach($related as $key => $val) { // related need to be loaded usng $related_name = end(explode('/', $key)); $related_model = $this->load_model($key.'_model'); $lookup_model = $this->load_model($val); $options = $CI->$related_model->options_list(); $field_values = (!empty($values['id'])) ? array_keys($CI->$lookup_model->find_all_array_assoc($CI->$related_model->short_name(TRUE, TRUE).'_id', array($this->short_name(TRUE, TRUE).'_id' => $values[$key_field]))) : array(); $fields[$key] = array('label' => ucfirst($related_name), 'type' => 'array', 'class' => 'add_edit '.$key, 'options' => $options, 'value' => $field_values, 'mode' => 'multi'); } } }
  • edited 7:32PM
    Do I do this with a fresh copy of MY_Model, or over my altered copy?
  • edited 7:32PM
    Fresh copy of MY_Model.
  • edited 7:32PM
    i have the same issue. I tried the fix suggested by admin but it still looks inside 'application' as far as i can see
  • edited 7:32PM
    patambrosio, were you able go get this to work with the fix above or did you implement your own change?
  • edited 7:32PM
    I'm sorry for the unacceptable delay here, I was sidetracked by another project. I just got to test it now, I replaced my fix with yours and the initial problem I had returned.
Sign In or Register to comment.