Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using ORM to populate select options
  • I have an ORM model that uses a foreign key to associate items to a single category. There is a has_many relation set up but I don't know how to get the ID and names for the select control when rendering the form. I can do this by hand, as it were, but I'm hoping it's something ORM could do itself and subsequently re-populate during editing. Perhaps something like this in my Model?: $_properties = array( 'category' => array( ... 'form' => array('type' => 'select', 'options' => {{from category table}} ) ); Thanks for your time.
  • Hi, I'm a recent convert from CodeIgniter trying to get this same functionality working in my app. Is there a way I could get a complete example?
  • The docs are pretty good: http://docs.fuelphp.com/packages/orm/intro.html
    This tutorial may also help: http://net.tutsplus.com/tutorials/php/easy-form-generation-using-fuelphp/
    I'll see if I can get some sort of example for you using what I've got.
  • You can make a function in your model which fetches the options from the database and that array to the property in the $_properties array. Make sure you execute the function in the init() method, so every time you instantiate that model the options will be loaded.
    public function get_options()
    {
        self::$_properties['options']['form']['options']  = \DB::select('id', 'option')->from('options')->execute()->as_array('id', 'option');
    }
    
  • Thank you - worked perfectly. self::$_properties = \DB::select('cat_id', 'title')->from('categories')->execute()->as_array('cat_id', 'title');
  • And if the options don't change very often, you can decide to cache the results: http://docs.fuelphp.com/classes/database/usage.html#caching
  • Nice, this was for an admin site but that's perfect for the public side. Thanks for the tip.

Howdy, Stranger!

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

In this Discussion