simple module - best way to show JOINed data

edited November 2013 in Modules
Hi all

I'm working on my first module which contains related data which would normally be selected via a query using a JOIN. Can anyone point to how best to display this form as one entity?

So. I have a table called [images] containing [id],[filename],[location_id]. I have another table called [locations] containing [location_id] (key), [lat],[long].

I have created a simple module to edit locations.

I would like to create a simple module for images but would like the related location data shown too with an option to edit it. I know how to use $foreign_keys for single phrase lookups (to give me a dropdown selection) but how do I tell the form_fields() function to show both sets of data as individual fields? How can I add an edit button for the locations portion of the dataset?

TIA

_croaker

Comments

  • edited 5:02AM
    Normally this is handled by having an "add / edit" button next to the field. This can be done in .93 by doing the following:
    $fields['my_field']['class'] = 'add_edit my_module';
    In 1.0, there is actually an "inline_edit" field type (http://docs.getfuelcms.com/general/forms#inline_edit) you can use. However, it sounds like you are wanting to flatten it into one set of fields for both. If so, there are a few things you may want to do:

    1. For modifying the list view of data of your module, you can do something like the following:
    // in locations_model function list_items($limit = NULL, $offset = NULL, $col = 'filename', $order = 'asc', $just_count = FALSE) { $this->db->select('locations.id, images.filename, lat,long, published'); // I added published just because FUEL will automatically not display those that aren't published $this->db->join('images', 'images.location_id = locations.id', 'left'); $data = parent::list_items($limit, $offset, $col, $order, $just_count); return $data; }

    2. For modifying the form fields of the module, you will need to add those additional fields in your model's form_fields method and save them to the database in an on_after_save model hook (http://docs.getfuelcms.com/general/models#hooks). You can access all the posted data through the model property of "normalized_save_data" (as opposed to the $values array passed to the hook which only has the values associated with that particular model). below is an example:
    // in locations_model
    function on_after_save($values)
    {
    // call the parent
    $values = parent::on_after_save($values);

    // if no category is selected, then we set it to the Uncategorized
    $saved_data = $this->normalized_save_data;
    if (!empty($saved_data['filename']))
    {
    $model = $this->load_model('images'); // 1.0 syntax... otherwise you'll need to use $this->load->model(...)
    $images_save['filename'] = $saved_data['filename'];
    $images_save['location_id'] = $values['id']; // this would be the location ID since this is the locations model.
    $model->save($images_save);
    }
    return $values;
    }
  • edited 5:02AM
    Thanks.

    It was the joined data for the form_fields() method I was having trouble loading until I noticed _common_query() (from the "The Categories to Articles Lookup Model" section of http://getfuelcms.com/user_guide/modules/tutorial) and realised I needed to put the select() and join() in there too.

    Works a treat.
  • edited 5:02AM
    Ah yes. _common_query will work for all find_{method}s and retrieving the data to edit for a record in the CMS uses the find_all_array method by default.
Sign In or Register to comment.