I am currently using Doctrine's DBAL library for MySQL interfacing and have currently built a structure around:
我目前正在使用Doctrine的DBAL库进行MySQL接口,目前已构建了一个结构:
public function update(array $data, $id)
{
return $this->db->update($this->table, $data, array('id' => $id));
}
Which of course returns the number of affected rows. The issue now is that I perform certain actions after updates that should trigger upon a successful update. Under the current system if I was to "edit" a record (showing the form only) and immediately hit save it returns an error since I check the result of the update function above. This not only displays an error in my site but also prevents the other "successful update" actions from running.
当然,这会返回受影响的行数。现在的问题是我在更新后执行某些操作,这些操作应在成功更新时触发。在当前系统下,如果我要“编辑”一条记录(只显示表格)并立即点击保存它会返回一个错误,因为我检查了上面更新功能的结果。这不仅会在我的网站中显示错误,还会阻止其他“成功更新”操作的运行。
Is there a way to see if an update failed to run outside of the affected rows? Can I just ignore this completely and assume the update will always work? Would trapping Exceptions be enough to catch any fatal errors?
有没有办法查看更新是否无法在受影响的行之外运行?我可以完全忽略这一点,并假设更新将始终有效吗?诱捕异常是否足以捕获任何致命错误?
3 个解决方案
#1
1
You can assume that update will always work. Exceptions will be thrown for major problems such as invalid field names or constraint violations. Consider making a few test cases just to convince yourself of this.
您可以假设更新始终有效。对于诸如无效字段名称或约束违规等主要问题,将抛出异常。考虑制作一些测试用例只是为了说服自己。
Be aware that no escaping is done with the update() method so it up to you cleanse your data.
请注意,使用update()方法不会进行转义,因此您可以清理数据。
#2
1
You can assume updates work for the most part but you probably want to look into Transactions, they are the best way of handling exceptions and maintaining DB integrity if something does go wrong. PHP doesn't provide conventional means of recovering from fatal errors for reasons outlined in this answers.
您可以假设更新大部分工作,但您可能希望查看事务,如果出现问题,它们是处理异常和维护数据库完整性的最佳方法。由于本答案中概述的原因,PHP不提供从致命错误中恢复的常规方法。
#3
1
Under the current system if I was to "edit" a record (showing the form only) and immediately hit save it returns an error since I check the result of the update function above
在当前系统下,如果我要“编辑”一条记录(仅显示表单)并立即点击保存,则返回错误,因为我检查了上面更新功能的结果
Don't forget that $this->db->update()
returns the number of the affected rows (or false on fail). If you just open "Edit" and click "Save", there will be no change of the data and "affected rows" will be 0. To return the success or fail of $this->db->update(...)
you may use false !==
不要忘记$ this-> db-> update()返回受影响行的数量(或失败时为false)。如果您只是打开“编辑”并单击“保存”,则不会更改数据,“受影响的行”将为0.要返回$ this-> db-> update(...)的成功或失败你可以使用假!==
if ($myModel->update($data, $id)) {
echo "success";
}
and in your myModel:
在你的myModel中:
public function update(array $data, $id)
{
return false !== $this->db->update(...);
}
#1
1
You can assume that update will always work. Exceptions will be thrown for major problems such as invalid field names or constraint violations. Consider making a few test cases just to convince yourself of this.
您可以假设更新始终有效。对于诸如无效字段名称或约束违规等主要问题,将抛出异常。考虑制作一些测试用例只是为了说服自己。
Be aware that no escaping is done with the update() method so it up to you cleanse your data.
请注意,使用update()方法不会进行转义,因此您可以清理数据。
#2
1
You can assume updates work for the most part but you probably want to look into Transactions, they are the best way of handling exceptions and maintaining DB integrity if something does go wrong. PHP doesn't provide conventional means of recovering from fatal errors for reasons outlined in this answers.
您可以假设更新大部分工作,但您可能希望查看事务,如果出现问题,它们是处理异常和维护数据库完整性的最佳方法。由于本答案中概述的原因,PHP不提供从致命错误中恢复的常规方法。
#3
1
Under the current system if I was to "edit" a record (showing the form only) and immediately hit save it returns an error since I check the result of the update function above
在当前系统下,如果我要“编辑”一条记录(仅显示表单)并立即点击保存,则返回错误,因为我检查了上面更新功能的结果
Don't forget that $this->db->update()
returns the number of the affected rows (or false on fail). If you just open "Edit" and click "Save", there will be no change of the data and "affected rows" will be 0. To return the success or fail of $this->db->update(...)
you may use false !==
不要忘记$ this-> db-> update()返回受影响行的数量(或失败时为false)。如果您只是打开“编辑”并单击“保存”,则不会更改数据,“受影响的行”将为0.要返回$ this-> db-> update(...)的成功或失败你可以使用假!==
if ($myModel->update($data, $id)) {
echo "success";
}
and in your myModel:
在你的myModel中:
public function update(array $data, $id)
{
return false !== $this->db->update(...);
}