MySQL中增加一列

发布于 2024-10-26 00:38:49 字数 225 浏览 5 评论 0原文

Zend 有没有办法将 MySQL 列中保存的整数加 1?

谢谢

编辑:

我当前的代码如下:

$row = $this->find($imageId)->current();
$row->votes = // THIS IS WHERE I WANT TO SAY INCREMENT
$row->save();

Is there a way in Zend to increment an integer, held in a MySQL column, by 1?

Thanks

edit:

My current code is like:

$row = $this->find($imageId)->current();
$row->votes = // THIS IS WHERE I WANT TO SAY INCREMENT
$row->save();

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

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

发布评论

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

评论(3

燕归巢 2024-11-02 00:38:49

是的,有。以下是使用产品并递增数量字段的示例:

$table     = 'products'; 
$data      = array('prd_qnty' => new Zend_Db_Expr('prd_qnty + 1')); 
$where[] = $db->quoteInto('pr_id = ?', $this->pr_id); 
$db->update($table, $data, $where);

Yes there is. Here's an example using products and incrementing a quantity field:

$table     = 'products'; 
$data      = array('prd_qnty' => new Zend_Db_Expr('prd_qnty + 1')); 
$where[] = $db->quoteInto('pr_id = ?', $this->pr_id); 
$db->update($table, $data, $where);
埋葬我深情 2024-11-02 00:38:49

votes++ 不能防止其他请求的竞争条件,这就是为什么需要一个数据库解决方案。

例如:假设两个请求几乎同时到达

1st request loads object - votes is 500
2nd request loads object - votes is 500
1st increments value in memory - votes is 501
2nd increments value in memory - votes is 501
1st saves to db - votes is 501
2nd saves to db - votes is 501 (should be 502)

votes++ does not protect against race conditions from other requests, that's why it is desirable to have a database solution.

For example: imagine two requests come in at almost the same time

1st request loads object - votes is 500
2nd request loads object - votes is 500
1st increments value in memory - votes is 501
2nd increments value in memory - votes is 501
1st saves to db - votes is 501
2nd saves to db - votes is 501 (should be 502)
最终幸福 2024-11-02 00:38:49

我可以提供以下建议吗

$row->votes++;

Might I offer the following suggestion

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