Add multiple entries in another table from a module - template form?

edited February 2014 in News & Announcements
First of all, thank you for this amazing CMS!

I'm currently using Fuel to build a site which lists music albums. So my database has an artist table, an album table and a track table.

What would be the best way to allow the user to add new tracks from the album module? It would be nice to have say 15 fields by default with the option to add more and reorder like the template form - http://docs.getfuelcms.com/general/forms#template

I was experimenting with template form but didn't know how to hook it up to another db table. Any tutorials/info on this?

Many thanks

Comments

  • edited 12:26AM
    oops wrong section...
  • edited 12:26AM
    So in this case, your album module's form_fields method would need to include the code to display the template field. The trickier part will be since you are not storing this in a single serialized field, you'll need to setup an on_after_save hook to store the track information for that album in the tracks table. Then in your form_fields method of your album model, you'll need to pull in the tracks for that album by loading the album and creating an array format that the template field will understand like so:
    function form_fields($values = array(), $related = array()) { ... $CI =& get_instance(); $CI->load->model('tracks_model'); $track_values = array(); if (!empty($values['id'])) { $track_values = $CI->tracks_model->find_all_array(array('album_id' => $values['id'])); // then set the 'value' => $track_values as a template field parameter } }
    See if something like that works...
  • edited 12:26AM
    Thank you very much. I have managed to display the track listing in the template form and I'm able to add new/delete tracks. I've implemented the on_after_save, however I'm not sure where to begin saving the update track information in the track table. Is there any tutorial or example code that might help?

    Thanks again
  • edited February 2014
    Ok I figured it out! Probably a better way but it works.

    function on_after_save($values) { parent::on_after_save($values); $new_tracklisting = $_POST['tracklisting']; //delete old tracks $this->db->where('album_id', $values['id']); $this->db->delete('tracks'); //add new tracks foreach($new_tracklisting as $track) { $track['album_id'] = $values['id']; $this->db->insert('tracks', $track); } }
Sign In or Register to comment.