I've just started using FuelPHP today and I've been making great progress but I've hit a problem that I haven't been able to move forward on for some time.
I have a table named 'book' which contains the field 'genre_id'. And a table named 'genre' holding the matching 'id' and genre 'value'.
I have a view/form (created by scaffolding) that allows you to create a new record in 'book', along with the genre_id. But I don't want the form to display the genre_id, I want it to display the joined genre value from the 'genre' table so that the user can see what it is. But then have it save the genre_id back to the 'books' table.
I suspect I'll need to create an array or key-value pairs (genre_id-genre), and use that in my form select box. But I'm not sure how to start with it.
Could anyone point me in the right direction? I've been looking at the documentation but I can't figure this out.
There are several solutions possible, depending on how many times you need to generate the fieldset, and how volatile your data is.
In general, you need it only once, when the fieldset is created. The best way to handle this is to add a method to your model called "set_form_fields".
public static function set_form_fields($form, $instance = null) { // create the base fieldset parent::set_form_fields($form, $instance);
// populate the dropdown options, with a blank first entry $form->field('dropdown')->set_options( \Arr::merge( array('' => ''), \DB::select('id', 'value')->from('table')->order_by('value', 'ASC')->execute()->as_array('id', 'value') ) ); }
It is called when you generate a fieldset from a Model definition, and allows you to customize the generated standard fieldset object.
Thanks Harro, I've added this code to my model and am looking at documentation trying to generate a fieldset from a model definition, as you've suggested. But I'm not having much luck.
Could you point me in the right direction another time?
If you have any to hand, it would be great to see an end-to-end example (ie "this goes in the model, this goes in the controller, this goes in the view" etc) so that I can visually follow the logic. If nothing like this exists, I will write up my solution once I have it working!
Appreciate your help with this (I'm fairly new to MVC and I'm reading up on it on the side).
This will generate a standard form, based on the form template in the config file. If you need more complex forms, you can do things in your view like:
// disable the form open/close tags for the fieldset, we'll do that manually $form->set_config(array('form_template' => '{fields}'));
// disable the id field for the remainder of the form $type == 'edit' and $form->disable('id'); $type == 'edit' and $form->disable('added'); $form->disable('session_id');