Consider the following code in a datamapper, to save data into a database:
/**
* Save data into the database
* @param Site_Model_User $model
* @throws InvalidArgumentException
*/
public function save($model) {
if (!$model instanceof Model_User) {
throw new InvalidArgumentException('Model is not of correct type');
}
$data = array();
$data['username'] = $model->getUserName();
$data['firstname'] = $model->getFirstName();
$data['lastname'] = $model->getLastName();
$data['email'] = $model->getEmail();
$data['password'] = $model->getPassword();
$data['role'] = $model->getRole();
$data['pic'] = $model->getImage();
if ($model->getId() < 0) {
// nieuw object, doe insert
$id = $this->getDao()->insert($data);
$model->setId($id);
} else {
// bestaand object, doe update
$where = $this->getDao()->getAdapter()->quoteInto('id = ?', $model->getId());
$this->getDao()->update($data, $where);
}
}
What i do is that i save my object everytime. When doing an insert this is no problem. Because I send a complete filled update. But lets say I want to update my password. And i do a update. It doesn't let me do the update because it says that certain values can not be 0.
For example: Mysqli statement execute error : Column 'username' cannot be null"
What is the best soultion for this? Should I first load the whole object again from the database, then change the fields and then do the update? Or are ther other better, more common solutions?
Thanks