It's called update_attributes:
http://www.phpactiverecord.org/docs/ActiveRecord/Model#methodupdate_attributes
$model = new Model;
$model->update_attributes($_POST); // implicit save()
Hi Ben,
Thanks for the reply. That is very very close to what I was looking for.
One drawback, however, is that the Model tries to update all items in the array, whether they are present in the underlying table or not. Ideally, the Model should only update the values for each item if there is a corresponding table column for each item.
This can work if you explicitly specify what fields you want to allow to be mass updated using $attr_accessible which is good practice to follow anyway.
http://www.phpactiverecord.org/docs/ActiveRecord/Model#var$attr_accessible
I can see doing as you suggest being a lot simpler so I need to think of the trade offs of allowing that.
Alternatively, you could use set_attributes_via_mass_assignment directly and pass false to the $guard_attributes param:
$model->set_attributes_via_mass_assignment($_POST,false);
$model->save();
Although a bit long winded, using $attr_accessible worked perfectly.
Thanks Kien.
(1-4/4)
Subject: Associate array insert/update saving
Hi,
Is it possible to send an associate array to the ->save() function, that automatically maps array keys to table columns?
I've just throw something together quickly, along the following lines.
I realise that I could do this manually for each upate/insert query, but just wondering if there is already some in built magic or plans to handle this?