I have the following database table
+----+---------+------+-------+
| id | title | code | views |
+----+---------+------+-------+
| 1 | Video 1 | abc | 1000 |
+----+---------+------+-------+
| 2 | Video 2 | def | 2000 |
+----+---------+------+-------+
| 3 | Video 3 | ghi | 3000 |
+----+---------+------+-------+
the model Videos.php
<?php
use Phalcon\Mvc\Model;
class Videos extends Model {
public $id;
public $title;
public $code;
public $views;
}
and the controller 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;
}
}
The above code should add +1 in views of the chosen video, but is always adding +2.
Ex: 1000, 1002, 1004, 1006 ...
If I run raw SQL the same thing happens. Am I doing something wrong?
Update #1
I activated the SQL query log and it is running 2 queries at the same time, so the values get duplicated, but I do not know why this.
In this link https://forum.phalconphp.com/discussion/274/model-save-create-two-records was suggested to check a table with the primary, but already exists, which is the id field.