form_fields() function with related tables (one to many relationship)

edited December 2011 in News & Announcements
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

  • edited 9:08PM
    If the page already knows the foreign_key value to insert, you could pass it as one of the $values to the form_fields method (first parameter) and set the value property on the field within the form_fields method.

    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
  • edited 9:08PM
    So, this is almost certainly a really stupid question -- coming from my inexperience with object-oriented programming.

    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.
  • edited 9:08PM
    I think what you may want to do is add a model hook to save your foreign key value. Check out another recent thread that may help:
    http://www.getfuelcms.com/forums/discussion/584/save-related-data-to-another-table/#Item_6
  • edited December 2011
    Very different problem. There one just needs to save info that comes from the form into two tables. It gets all the info from the form and the save. An after save hook works for that.

    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?
  • edited 9:08PM
    For that to work with 'create', the value for the foreign key needs to be in the posted data already. So setting it in the form_fields method (like a hidden field as you mentioned), won't work for you?

    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.
  • edited 9:08PM
    So this is where the problem is likely my ignorance of object-oriented programming. And I'm fairly sure this is probably something that is so simple that you're going to think I'm really stupid ...

    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
  • edited December 2011
    I still have no idea how the Fuel team intended to have something like this work, but I decided to see if I could pass it with a session variable followed up by using the form_fields method to set the default and hide the field if the either the session variable or the POST was set.

    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.
Sign In or Register to comment.