Related Fields not supported in Advanced Modules
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
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');
}
}
}
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'); } } }