What is the proper way of editing foreign keys in-line in the CMS?

edited December 2017 in Modules
I have a table with around 20 columns. Say the name is 'table1'.

I've been thinking of changing that so that each entry in the table has links to 20 rows in a different table ('table2'), which is basically a key/value store (so contains three columns only: table1_id, key, value).

If I did that using `$has_many` in my `Table1_model`, I'd probably end up with 20 dropdown menus, and editing each of them would open up a new modal window etc, requiring quite a lot of mouse clicks.

Is there an easier way to do this? Or do I need to manually load everything from the database in `form_fields`, set up the field names ensuring they're distinct, and then insert/update rows in `table2` in one of the "hook" methods?

That's essentially what I did in my "translatable" module:

1)
https://github.com/martynassateika/FUEL-CMS-Translatable-Module/blob/0.2/models/Translatable_item_model.php#L47

2)
https://github.com/martynassateika/FUEL-CMS-Translatable-Module/blob/0.2/models/Translatable_item_model.php#L176

Is there a more elegant way to do this in FuelCMS?

Comments

  • edited 4:50PM
    There is a $foreign_keys = array(); property on the model you can set to the column name(s) that are foreign keys to other models.
  • edited 4:50PM
    Hey David, thanks for the reply. Wouldn't using $foreign_keys yield the same result thugh? I mean the database structure would be slightly different as it'd use the fuel_relationships table, but in terms of display, the fields would look the same either way right?

    I dug around the source code a bit and found that when layout variables are displayed in the `Pages` view, it does kind of something similar where the variables are all POSTed with names pre-fixed with a special string (I think it was `vars--`), and then re-processed on the other end.

    I guess stripping that functionality out would be quite a task though :-( As I understand Layouts in FUELCMS are only used for pages, and cannot be used in any module.
  • edited 4:50PM
    That's correct about layouts to some extent. There are block layouts that can be used for modules. But the template field type may also be of some use for you because you could essentially create a repeatable template that has all the fields you need listed and you won't have to click to open a modal to edit. You would have to do a database query to pull in the values from your table to populate each repeatable fields "values" parameter:
    http://docs.getfuelcms.com/general/forms#template
    http://docs.getfuelcms.com/general/forms#block
  • edited 4:50PM
    Thanks a lot for the pointers. I'll give this a go soon and report back :-)
Sign In or Register to comment.