Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Access the $_properties name
  • Hi, One of my models (extends ORM) has a lot of $_properties. After the validation of my form, I'd like to save the input without doing this for all the properties:
    $vn = Model_Stockvn::find($id);
    $vn->name = Input::post('name');
    $vn->title = Input::post('title');
    $vn->brand= Input::post('brand');
    $vn->year= Input::post('year');
    ....
    ....
    

    I tried that :
    $vn = Model_Stockvn::find($id);
    foreach ($vn::$_properties as $value) {
     ($value != 'id') and $vn->$value = Input::post($value);
    }
    

    But it can't not working because it can't access protected property of the model. Any ideas? I'm quite new in programming so it's OK you can tell me if my question is too stupid. Hope my question is clear. Thx.
  • Since 1.3 you can set a complete array with set():
    $data = \Input::post();
    $model->set($data)->save();
    

    If you're using 1.2 you use values() instead of set(). Make sure every key in the array is a property of the model (or of one of its relations). If you use models with Model_Crud, this is not necessary (it just ignores keys which are no property of the model). https://github.com/fuel/orm/blob/1.3/develop/classes/model.php#L925
  • thank you for your quick reply. It works great. I got to dig more by myself. I don't have the reflex enough to look at inside the existing code of Fuel.
  • Gonab wrote on Friday 20th of July 2012:
    thank you for your quick reply. It works great. I got to dig more by myself. I don't have the reflex enough to look at inside the existing code of Fuel.

    Since it's not in the docs, I don't blame you ;) Tiny trick, if you have a form with for example 10 fields, but only 9 of them are properties of the model, you can use:
    $data = Input::post();
    unset($data['this-is-posted-but-not-a-property']);
    $model->values($data)->set();
    

    That's better than:
    $model->prop1 = Input::post('prop1');
    $model->prop2 = Input::post('prop2');
    ....
    

    (if you use Model_Crud you can skip the unset() line.)
  • Tiny trick, if you have a form with for example 10 fields, but only 9 of them are properties of the model, you can use:
    $data = Input::post();
    unset($data['this-is-posted-but-not-a-property']);
    $model->values($data)->set();
    

    Thanks for the tip. Already done to skip the submit button.

Howdy, Stranger!

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

In this Discussion