We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

EE 3.1: Using Model::fill() on existing item and saving

Developer Preview

Manuel Payano's avatar
Manuel Payano
144 posts
9 years ago
Manuel Payano's avatar Manuel Payano

Hi,

If you have an existing model object, you can use Model::fill() to quickly set a bunch of properties. (setters are skipped).

But if you use Model::save() on the object, none of the dirty properties are saved, they are simply ignored.

When saving, it seems that no dirty properties are found.

Example Code

$form = ee('Model')->get('forms:Form', $form_id)->first();
$form->fill($postData);
$form->save(); // Nothing is saved

foreach ($postData as $key => $val) {
    $form->setProperty($key, $val);
    $form->{$key} = $val;  // Same Thing
}

$form->save(); // Works as expected

Is this the expected behavior? Is there another way?

       
Manuel Payano's avatar
Manuel Payano
144 posts
9 years ago
Manuel Payano's avatar Manuel Payano

After some further digging i found the answer.

Entity::set() does the job also.

$form = ee('Model')->get('forms:Form', $form_id)->first();
$form->set($postData);
$form->save(); // Works as expected
       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
9 years ago
Pascal Kriete's avatar Pascal Kriete

Looks like you found it! For reference, fill is what we use to hydrate the model from the db.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.