如何更新数据库中的数据
考虑数据映射器中的以下代码,将数据保存到数据库中:
/**
* 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您必须从数据库加载所有字段并填写除密码字段之外的所有必填表单字段,然后更改密码并提交表单。这将帮助您验证必填字段不为 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.
Chandres maheshwari 的答案是一个有效的选择,但请将此视为替代方案。
我不熟悉您正在使用的框架,但是您不能自己构建查询,或者至少选择要更新的字段吗?
我会做类似的事情:
当然,您应该检查字段是否为 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:
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.