It looks like you're new here. If you want to get involved, click one of these buttons!
if (!empty($related))
{
$key_field = $this->key_field();
if (is_string($key_field))
{
foreach($related as $key => $val)
{
// related need to be loaded using slash syntax if model belongs in another module (e.g. my_module/my_model)
$related_name = end(explode('/', $key));
$related_model = $this->load_model($key.$this->suffix);
$related_model_name = $related_name.$this->suffix;
$lookup_name = end(explode('/', $val));
$lookup_model = $this->load_model($val);
$options = $CI->$related_model_name->options_list();
// important to sort by id ascending order in case a field type uses the saving order as how it should be returned (e.g. a sortable multi-select)
$singular_name = $this->singular_name(TRUE);
$field_values = (!empty($values[$key_field])) ? array_keys($lookup_model->find_all_array_assoc($singular_name.'_id', array($singular_name.'_id' => $values[$key_field]), 'id asc')) : array();
$fields[$key] = array('label' => ucfirst($related_name), 'type' => 'multi', 'module' => $key, 'options' => $options, 'value' => $field_values, 'mode' => 'multi');
}
}
}
$field_values = (!empty($values[$key_field])) ? array_keys($CI->$lookup_name->find_all_array_assoc($singular_name.'_id', array($singular_name.'_id' => $values[$key_field]), 'id asc')) : array();
Comments
How would I tell form_fields() which data to show here?
TIA...
As for saving, you may need to use the save_related method in a model hook (on_after_save). Or better yet, if you are using the 1.0 beta, you can use FUEL's built in relationship tables to automatically do this without the need for any additional tables (using has_many... if you have seen this yet, I'd highly recommend looking into it):
http://docs.getfuelcms.com/general/models#relationships
I have a feeling there's still a problem with that original code block in my opening post.
$field_values = (!empty($values[$key_field])) ? array_keys($CI->$lookup_name->find_all_array_assoc(...
I'm over-riding find_all_array_assoc() (in conjunction with _common_query() in the same model) to return my set of values to use on the right-hand side of the form field.
function __construct() { parent::__construct('resolve_items'); } function _common_query() { $this->db->select('righthandvalue'); } function find_all_array_assoc($assoc_key = 'id', $where = array(), $order_by = NULL, $limit = NULL, $offset = NULL) { $result = $this->find_all(array('resolve_itemsID'=>current($where)),'resolve_itemsID ASC',$limit,$offset,'array',''); return $result; }
and which returns
Array ( [0] => Array([righthandvalue] => 6) [1] => Array([righthandvalue] => 1) )
as a result of the array_keys() in that MY_Model.php code block, $field_values ends up being:
array_keys(): Array ( [0] => 0 [1] => 1 )
when in actual fact it needs to be:
Array ( [0] => 6 [1] => 1 )
Is the MY_Model.php code block not doing what it should or is my find_all_array_assoc() and _common_query() at fault?