Module with dependent sub-model for one-to-many relationship
I have a module that defines a "meeting" event. It has a bunch of normal fields (title, start date, end date, etc.). However, each meeting can have an arbitrary number of attached files (meeting minutes, presentation files, photos of the meeting). Those files do not fit "in" the meeting without making a lot of things unwieldy. It is straightforward (from a DB design perspective) to make a meeting_attachments table that has a foreign_key out to the meeting's ID and then has an arbitrary number of rows for each meeting.
It seems like the appropriate place to ADD a meeting attachment is on the meeting's "detail" page. And it's straightforward to have a
fuel_edit('create', 'Add Attachment', 'meeting_attachments');
call in the meeting's "detail" page. But as far as I can tell, there's no way for me to pass the meeting_id in to the meeting_attachments create page, so I can't establish the relevant foreign_key.
This seems to be the same problem that was discussed here:
http://www.getfuelcms.com/forums/discussion/587/form_fields-function-with-related-tables-one-to-many-relationship/ but there was never an answer to that question. Am I missing something? Is there a better way to do this?
Comments
$this->session->set_userdata('meeting_id', $meeting->id);
from my VIEW, right after I obtain $meeting with a fuel_model( ... ) call, and then pulling it back out with
$this->session->userdata('meeting_id');
in the form_fields() function of the meeting_attachments model. But this feels sketchy for a bunch of reasons, including that (from my understanding), those are both happening in the same request (the entire VIEW gets parsed, putting a placeholder in where fuel_edit( ... ) was, and then another pass goes through to replace the placeholders where appropriate. I don't really remember how CI handles session userdata within a single request, so I don't know how trustworthy this technique is. And it seems a little bit backwards anyhow.
It is (again) straightforward to add a method to the meeting Record Object that calls out to a method in the meeting_attachments model, but I don't see how to get the items that are returned to be Record Objects.
With regards to the presentation side, you should be able to create a method like you suggested on the meeting_model record class that can return all the meeting attachment objects (assuming you have setup a meeting_attachments_model and a corresponding meeting_attachment_model... note the no "s"... record class):
// in your meeting_model class function get_attachments() { return $this->lazy_load($this->id, 'meeting_attachments_model', TRUE); }
What about if you specifiy the column to match against with:
return $this->lazy_load(array('your_id_field' => $this->id), 'meeting_attachments_model', TRUE);
^ I have many examples where that works for me.