【发布时间】:2017-05-27 02:46:00
【问题描述】:
我有以下数据库表
+----+---------+------+-------+
| id | title | code | views |
+----+---------+------+-------+
| 1 | Video 1 | abc | 1000 |
+----+---------+------+-------+
| 2 | Video 2 | def | 2000 |
+----+---------+------+-------+
| 3 | Video 3 | ghi | 3000 |
+----+---------+------+-------+
型号Videos.php
<?php
use Phalcon\Mvc\Model;
class Videos extends Model {
public $id;
public $title;
public $code;
public $views;
}
和控制器IndexController.php
<?php
use Phalcon\Mvc\Controller;
class IndexController extends Controller {
public function indexAction(){
$watch = Videos::findFirst(3);
// add +1 to video views
$watch->views = $watch->views + 1;
// update video data
$watch->save();
// output video data to view
$this->view->watch = $watch;
}
}
上述代码应在所选视频的观看次数中添加 +1,但始终添加 +2。
例如:1000、1002、1004、1006 ...
如果我运行原始 SQL,也会发生同样的事情。 我做错了吗?
更新 #1
我激活了 SQL 查询日志,它同时运行 2 个查询,所以值会重复,但我不知道为什么会这样。
在此链接中,https://forum.phalconphp.com/discussion/274/model-save-create-two-records 被建议检查主表,但已经存在,即 id 字段。
【问题讨论】:
-
也许将解决方案作为答案? :)
-
完成@Nikolay Mihaylov。