Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Use select field from within ORM
  • Hello,

    I'm migrating my applications from Kohana to FuelPHP and till now i love FuelPHP.

    I came across the possibilty to create a form from your ORM properties using Fieldset.
    For the most fields this is working like it supposts to work exect from the select field.

    I'm trying to generate the select values from table data.

    At this moment i did not found a way to get this working.

    I tried a lot of things but everything ended up not working.

    I have a table named Members and a Table named Dues.
    The member has a record dues_id that should be connected to Dues id

    Can anyone help?
  • HarroHarro
    Accepted Answer
    Code generation currently doesn't support relations, so you'll have to add the dropdowns manually after generation.

    You're field definition in the model should look like this:

            'language'            => array(
                'label'            => 'model_session.language',
                'form'          => array(
                    'type'        => 'select',
                ),
                'default'        => 'EN',
            ),

    this will tell the fieldset it's a select, and what the default value on an emtpy form is.

    To load the data, there are multiple options, depending on what it is. If it's (virtually) static data, like available languages in this example, I load it in the models _init() method, using

        public static function _init()
        {
            // load the available languages for this app from the settings table
            static::$_properties['language']['form']['options'] = \Settings\Load::settings('LANGUAGE', null);
        }

    (note: settings is a package we use in our apps, it returns a key-value array, 'EN' => 'English')

    If it is dynamic data, you would want to load it before generating the form. In that case you do it in set_form_fields() method:

        public static function set_form_fields($form, $instance = null)
        {
            // add the csrf token field to the form
            $form->form()->add_csrf();

            // create the base fieldset
            parent::set_form_fields($form, $instance);

            // add the meta data correspondence fields
            foreach (array('mailcalled', 'mailresults', 'mailcerts', 'mailinvoice') as $field)
            {
                $form->field($field)->set_options(\Settings\Load::settings('MAILTO', null));
            }
        }

    (again, this is a specific example from one of our apps)
  • Thanks this is working.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion