form_fields() function with related tables (one to many relationship)
I'm not understanding the examples in the Forum or the documentation ...
I have two related tables (alerts and alerts_updates) with a foreign key referencing alerts in the alerts_updates
I gritted my teeth and made them both INNODB with appropriate indices, constraints, and cascade on delete
It is a one-to-many (one alert with many updates) relationship.
I'm trying to keep this as a simple module and also not create my own controller ..
I'm planning to have alerts_updates edited from a page where I already know the foreign key.
I'm assuming I need to do nothing for "edit" since the foreign key is already in the alerts_updates record
Is there a way to get it set for "add" ? The page knows the value, so can I get it to my form_fields function in my model?
Similar question if editing from Admin ... though I wouldn't cry if I had to have a pull-down or similar for that.
Thanks! (And thanks for responding to quickly to all of my questions!)
Comments
For the admin, if you set the foreign_key property on the model, it will treat it as a pull-down. Also, there is an "edit_method" module parameter that can also be used to set the values for you record. The default is find_one_array, however, you can create your own method and set it to whatever values you initially want for your record:
http://www.getfuelcms.com/user_guide/modules/simple
I'm triggering all this from the view using
<?=fuel_edit('create', 'New Update', 'alert_updates');?>
Don't I somehow need to pass the foreign key value to that? Seems like it is the inline_edit page that has to pass the value to form_fields. Same issue with edit_method .... If I were actually calling form_fields from my code, I'd be in good shape.
By the way, the drop-down works perfectly.
Thanks for your patience.
http://www.getfuelcms.com/forums/discussion/584/save-related-data-to-another-table/#Item_6
My problem is passing a value to the page that creates the form (it should then just be a hidden field).
When updating a field, fuel_edit has to pass the value of the id to module_inline_edit (it does, as the first parameter)
My problem is comparable.
When creating a field on the linked table, I need to pass the value of the foreign key to module_inline_edit "create" and it needs to know how to use it.
I've studied the code for fuel_edit and the code in module.php...I don't see anything to handle extra parameters ...
I also find nothing comparable in any of the examples -- all of those make the user know the value and select it...or make the relationships many-to-many and make them select.
Any other thoughts?
You probably already know this, but the first parameter you can pass to the form_fields method the $values parameter. By default, it uses the model's "find_one_array" method to provide any saved data values to the form. This can be overwritten by changing the "edit_method" property of your module to a different method on your model. That method could by default return the foreign key value you need if no valid data exists for the passed record $id to that method.
I know that I can use the methods mentioned if I can get the saved value TO the form_fields method or the edit_method. The question is more like, "how do I get them to it?" -- everything I try to do to get these set throws php errors
With html for the view removed...
<? foreach($alerts as $alert) : ?> <?$ alerts_id = $alert->id; ?> <? $alert_updates = fuel_model('alert_updates',array('where'=>'alerts_id='.$alerts_id)); ?> <? foreach($alert_updates as $update) : ?> <?=fuel_edit($update->id, 'Edit Update', 'alert_updates'); ?> <? endforeach; ?> ............. Values parameter (alerts_id) needs to get set HERE ............. <?=fuel_edit('create', 'New Update', 'alert_updates'); ?> <? endforeach; ?>
For
<?=fuel_edit($update->id, 'Edit Update', 'alert_updates');?>
I have the id, and I can use the methods you suggest to get the value of $alerts_id. And those can then be used to eradicate the drop-down from the form and replace with a hidden field ...
For
<?=fuel_edit('create', 'New Update', 'alert_updates');?>
I don't have the info to use find_one_array or the edit_method without somehow passing the value of $alerts_id. How do I get the $values parameter set ? I know it needs to go where the comment is, but I'm clueless on what the actual code needs to be.I think this boils down to --how do I set the $values parameter for a form that isn't being built until the next page loads given that the fuel_edit ('create') doesn't let me pass the value as a POST or GET
That works.
In my mind, fuel_edit and the code for inline_edit should allow an optional array parameter to set defaults for "create" -- the obvious use being to set foreign keys. It looks easy to do, just not doable, at least within my limited knowledge, without altering both the fuel_edit and inline_edit code.
Unless I missed some fundamental concept (quite probable), this would be an enhancement suggestion.