Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to use relations with fieldset
  • Hello!
    I'll try to describe my question as I can, but sorry for my language - English is not my native.


    We have two tables in database - brands and models. For example, brands are Audi, BMW and others and models for Audi are A4, A6 and so on. 
    Table brands has 2 fields - table_id, name
    Table models has 3 fields model_id, table_id, name

    I want to make a form to fill table models. I made a form to fill the table brands with help of this article: http://net.tutsplus.com/tutorials/php/easy-form-generation-using-fuelphp . But there were no relations with tables.

    In new form I want to make field select, options of which are names from table brands with appropriate brand_id. But I don't know how to do it correctly in FuelPHP.

    So, that is the question, how to do such select field with options from another table
  • Depends on if you use the ORM or not.
  • Yes, I use ORM
  • HarroHarro
    Accepted Answer
    An ORM model has a method called set_form_fields(), which is called by your fieldset object to define the fields of the fieldset based on the model's property definition.

    You can overload this method in your model (by default it's provided by Orm\Model), and use that to populate the options at runtime.

    Assuming you have a field called 'brand_id', which is defined in the form section of the properties as type 'select', and you have a relation called 'brands', you can do

    public static function set_form_fields($form, $instance = null)
    {
        // call the parent to create the fieldset
        parent::set_form_fields($form, $instance);

        // get the options for brand using a DB query if you need all of them
        $options = \DB::select('id', 'name')->from('brands')->execute()->as_array('id', 'name');

        // or use ORM access if you only need related items
        $options = \Arr::assoc_to_keyval($this->brands, 'id', 'name');

        // set the options on the brand_id field
        $form->field('brand_id')->set_options($options);
    }
    So you use either one of them, depending on what you need in the dropdown.
  • Thank you very much! I'll try to do like you wrote above!
    UPD! It works!!!! Thank you once more!!!
  • I think that my second question is based on this problem description, so I didn't start new topic.
    Now we have another page where brands and models are together. As we have many models for every brand it will be convenient to load models with AJAX - we choose Audi and all models of Audi are loaded. I don't know, from which side I can approach to this point in FuelPHP.

    May be someone can turn me the right way? (Should I write my own JS code which will insert options in the form or may be FuelPHP have such possibilities (and I should insert some parameters somewhere). Where it will be correctly to create a function, which returns id and model name. Or, may be I can simply use FuelPHP functionality for this part too). 
  • Given the fact that you know the PK of the parent (the 'audi' record), and all models contain a FK which is equal to this PK value (otherwise they are not related), I would query the model table directly:

    $result = Model_Models::query()->where('brand_id', '=', $brand_id)->get();

    You can use the REST controller to create the API method for your ajax call. Have it return json so that your js code can use that to update the dropdown client-side.
  • It's great! Thank you!

Howdy, Stranger!

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

In this Discussion