Doctrine: Set model values as array
Question
I've got an array of values I want to update my model with.
Doctrine_Access provides a function setArray which is nearly exactly what I need - except it cares about values that don't have fields in the model. I want those to be ignored.
A little example. Say we have a User table with the field username.
$user = new User();
$user->setArray(array('username'=>'xyz'))->save();
That would work!
$user = new User();
$user->setArray(array('username'=>'xyz','anotherKey'=>'anotherValue'))->save();
That doesn't. I want Doctrine to just ignore anotherKey, if there is no related field. The intention is, that I don't want to filter my arrays before I update my model.
What is the cleanest and easiest way to get this done?
Solution
Doctrine_Record::fromArray() solves it. Unfortunately it doesn't return the object, so it's useless for method chaining...
OTHER TIPS
this is useful
add find method to model:
class Address extends Doctrine_Record {
public static function factory() {
return new Address();
}
public function findById($id) {
$findObject = Doctrine::getTable('Address')->findOneByid($id);
return $findObject;
}
....
and use it
$address = Address::factory()
->findById(13)->set('name', 'new data')->set('anotherfield','another data')->save();