duanfu1945 2010-12-06 10:06
浏览 52
已采纳

如何更新数据库中的数据

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

  • 写回答

2条回答 默认 最新

  • dopt85756 2010-12-06 10:12
    关注

    yes, you have to load all the fields from the database and fill up all the required form fields except password fields and then change your password and submit the form. this will help you for validate required field not NULL and go further.

    Thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏