Wrong resolution of has_many key

edited August 2015 in Modules
Hi,

I have 2 modules with a pretty similar has_many relationship. There is one base module and every entry can have n images assigned. The images are stored via another simple module and the assignment is stored in the relationship table.

In the image module I have defined. the vice-versa relationship via belongs_to.

Now when I open an entry of the "fahrzeug_images_model" the correct "fahrzeug" entry is selected and displayed in the select field via the display_field defined in the module configuration.

When I open an entry of the other module "einsatz_images_model" in the select field there are displayed some numbers but not the correct IDs from the image module. I have also defined a display_field, but this is not displayed...

Correct working module:
$config['modules']['fahrzeuge'] = array( 'module_name' => 'Fahrzeuge', 'instructions' => lang("fahrzeuge_instructions"), 'permission' => 'vehicles', ); $config['modules']['fahrzeug_images'] = array( 'module_name' => 'Fahrzeugbilder', 'display_field' => 'description', 'permission' => 'vehicles', );
class Fahrzeuge_model extends Abstract_module_model { public $has_many = array('fahrzeug_images' => 'fahrzeug_images_model'); public $belongs_to = array('missions' => 'missions_model');
class Fahrzeug_Images_model extends Abstract_module_model { public $required = array('description', 'fahrzeug_id'); public $foreign_keys = array('fahrzeug_id' => 'fahrzeuge_model');

Not working module:
$config['modules']['missions'] = array( 'module_name' => 'Einsätze', 'instructions' => lang("einsatz_instructions"), 'permission' => 'missions', ); $config['modules']['mission_images'] = array( 'module_name' => 'Einsatzbilder', 'display_field' => 'description', 'instructions' => lang("einsatz_images_instructions"), 'permission' => 'missions', );
class Missions_model extends Abstract_module_model { public $required = array('name', 'datum_beginn', 'uhrzeit_beginn', 'datum_ende', 'uhrzeit_ende', 'lage', 'anzahl_kraefte', 'bericht', 'cue_id', 'type_id'); public $foreign_keys = array('cue_id' => 'mission_cues_model', 'type_id' => 'mission_types_model'); public $has_many = array('mission_images' => 'mission_images_model', 'fahrzeuge' => 'fahrzeuge_model'); public $filters = array('name', 'datum_beginn', 'ort');
class Mission_Images_model extends Abstract_module_model { public $required = array('description', 'mission_id'); public $foreign_keys = array('mission_id' => 'missions_model');

You see anything what could cause such a behaviour?

Comments

  • edited August 2015
    Try overriding the model's options_list method:
    public function options_list($key = 'id', $val = 'description', $where = array(), $order = TRUE, $group = TRUE) { if (empty($val)) { $val ='description'; } $data = parent::options_list($key, $val, $where, $order); return $data; }
  • edited 6:07AM
    Ok... But why is it working in one module and not in the other.
  • edited 6:07AM
    The options_list by default will try and find the first non "_id" field to use as the label if it's not specified in the method call. The has_many method uses that method without the parameters passed to it (so the default behavior).
  • edited August 2015
    hmm ok... another question... is it possible to have the options_list be built of multiple values?

    So if I open the Mission Image the options list value should be datum_beginn - name

    Is that possible?
  • edited 6:07AM
    You can pass in a $key and $val parameter to the options_list if you are using it ouside of relationships. If the options are generated by a relationship, you can simply overwrite the 'options' parameter in your form fields array to use whatever you need:
    $fields['my_field']['options'] = $this->my_model->options_list('datum_beginn', 'name');
  • edited August 2015
    I think there was a misunderstanding.
    The key should stay the id field. But the displayed value should consist of 2 database fields.

    The problem is, that in my table I have many entries with the same name so I cannot differentiate between them in the select box... I want to have a combined value of the datum_beginn field and the name field.

    I think I will overwrite the options_list method with a whole new logic.
  • edited 6:07AM
    public function options_list($key = 'id', $val = 'name', $where = array(), $order = TRUE, $group = TRUE) { $this->db->order_by('datum_beginn desc, uhrzeit_beginn desc, id desc'); $this->db->select('id, name, datum_beginn, uhrzeit_beginn'); $query = $this->db->get('missions'); foreach($query->result() as $row) { $data[$row->id] = get_ger_date($row->datum_beginn).' '.$row->uhrzeit_beginn.' - '.$row->name; } return $data; }
    solved the problem!
  • edited 6:07AM
    @kingmaoam Thank you!
Sign In or Register to comment.