Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Fieldset properties for ORM model, fetch from DB
  • I've read through through this post (http://fuelphp.com/forums/discussion/6997/orm-fieldset-and-the-form-builder), but I can't get it to work in my controller for some reason.

    Goal: update model properties for fieldset fields (option values) after _init. 

    Controller:

     $form = Fieldset::forge('form_newjob_step2');
            
     $model_job = \Model_Job::forge();
     $model_job::init_crud(); // sets the _properties

     // if checked here, $model_job has updated properties, as set in init_crud()

    $form->add_model($model_job);

    View:
    Now, when I build the form element with <?php echo $form->field('website_id')->build(); ?> it doens't have options.

    Whenever I call the model's init_crud method from the model's _init() method, it would work.

    Any ideas?



  • HarroHarro
    Accepted Answer
    Is this perhaps not the first time you use a fieldset for that model? Fieldsets are cached, so a second call will return the first fieldset.

    The options ($_property[fieldname]['form']['options']) are processed before adding the field to the fieldset, so I don't see an immediate reason why the would not end up in the fieldset.

    p.s. if you want to set data at runtime, it's better to overload set_form_fields() in your model. It's called by add_model() to create the fieldset. Have it call the parent, and use the fieldset returned to set runtime values on it. Then it happens automatically, without your controller having to call something...
  • It's a quite simple method, in which the fieldset gets forged right after the opening bracket of the function.

    public function action_new_job_step2()
        {
            $form = Fieldset::forge('form_newjob_step2');
            
            $model_job = \Model_Job::forge();
            $model_job::init_crud();
            
            $form->add_model($model_job);

        (...)
        }

    And init_crud in the model:

     public static function init_crud()
            {
                // Fetch websites and categories. 
                static::$_properties['website_id']['form']['options'] = DB::select()->from('websites')->execute()->as_array('id', 'name');
                
            }

    This in response to the first part. As you say in your PS that overloading of set_form_fields is the way to go, I'll go with that. Thanks for your reply!

Howdy, Stranger!

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

In this Discussion