Fatal error: Call to a member function find_all_array_assoc()

edited December 2013 in Modules
Hi

I'm confused about this error I get when setting up a relationship in my simple module (similar to articles/categories/categories_to_articles example)

The full error is
"Fatal error: Call to a member function find_all_array_assoc() on a non-object in /var/www/fuel/modules/fuel/core/MY_Model.php on line 2729"
The MY_Model.php code-block is
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'); } } }
and the error relates to "$lookup_model->find_all_array_assoc(". $lookup_model seems not to be an object when assign by "$lookup_model = $this->load_model($val);". $related_model seems to be created in a similar fashion however it's not used. The "$options = $CI->$related_model_name->options_list();" style of calling the method is used instead.

If I edit My_Model.php and use this instead - it works.
$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();

I've not tried the examples with the original code. This became an issue for me where I had to over-ride find_all_array_assoc() so I could supply my own fieldnames - they bear no resemblance to those needed to make Fuel work automatically.

I guess I'm just flagging this up in case it's a bug. My edit works for me but will break if I do a git pull.

Comments

  • edited 4:57AM
    Hmm. Further to this situation, I'm able to populate the left side of the multi-select widget and clicking multiple selections across to the right will give me the correct values in $this->normalized_save_data. I don't seem to be able to populate the right-hand side when opening the form for an existing record that should have right-side data to show though.

    How would I tell form_fields() which data to show here?

    TIA...
  • edited 4:57AM
    That first example appears to be a bug. The "load_model" method only returns a string of the name of the modal so your fix should work. I'll post that in a bit.

    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
  • edited 4:57AM
    Thanks. has_many() isn't appropriate for this situation though I can see the merits.

    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?
  • edited 4:57AM
    Ah. Fixed. Predictably enough it was my find_all_array_assoc() which didn't have anything in the last parameter of the find_all(). I needed the $assoc_key which in my example here is 'righthandvalue'.
Sign In or Register to comment.