如何更新数据库中的数据

发布于 2024-10-06 05:24:40 字数 1255 浏览 7 评论 0原文

考虑数据映射器中的以下代码,将数据保存到数据库中:

 /**
 * 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);
    }
}

我所做的是每次都保存我的对象。执行插入时这没有问题。因为我发送了完整的更新。但假设我想更新我的密码。我做了一个更新。它不允许我进行更新,因为它说某些值不能为 0。

例如: Mysqli 语句执行错误:列“用户名”不能为空”

对此最好的解决方案是什么? 我应该首先从数据库再次加载整个对象,然后更改字段,然后进行更新吗?或者还有其他更好、更常见的解决方案吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笛声青案梦长安 2024-10-13 05:24:40

是的,您必须从数据库加载所有字段并填写除密码字段之外的所有必填表单字段,然后更改密码并提交表单。这将帮助您验证必填字段不为 NULL 并进一步进行。

谢谢。

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.

一紙繁鸢 2024-10-13 05:24:40

Chandres maheshwari 的答案是一个有效的选择,但请将此视为替代方案。

我不熟悉您正在使用的框架,但是您不能自己构建查询,或者至少选择要更新的字段吗?

我会做类似的事情:

public function save($model) {
    if (!$model instanceof Model_User) {
        throw new InvalidArgumentException('Model is not of correct type');
    }
    $data = array();
    if($model->getUserName())
        $data['username'] = $model->getUserName();
    if($model->getFirstName())
        $data['firstname'] = $model->getFirstName();

    // .... and so on ...

    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);
    }
}

当然,您应该检查字段是否为 false/NULL,因为它尚未被修改,或者因为用户实际上希望它为空(即删除),或者确保在插入时所有字段都不为 NULL 。

Chandres maheshwari's answers is a valid option, but consider this as an alternative.

I'm not familiar with the framework you are using, but can't you build your query yourself, or at least select the fields you want to update?

I would do something like:

public function save($model) {
    if (!$model instanceof Model_User) {
        throw new InvalidArgumentException('Model is not of correct type');
    }
    $data = array();
    if($model->getUserName())
        $data['username'] = $model->getUserName();
    if($model->getFirstName())
        $data['firstname'] = $model->getFirstName();

    // .... and so on ...

    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);
    }
}

Of course you should check whether a field is false/NULL because it has not been modified or because the user actually wants it empty (i.e. deleted) or make sure that all the fiels are not NULL when you are inserting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文