2

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.

2
  • Maybe move the solution as answer? :) Commented May 27, 2017 at 11:21
  • 1
    done @Nikolay Mihaylov. Commented May 27, 2017 at 12:10

1 Answer 1

0

Solution by this response https://stackoverflow.com/a/35770447/2587280

The error occurred because Phalcon needs a .htaccess file that redirects all requests toindex.php. Using Google Chrome, by default it tries to load a favicon.ico file from the root of the site, but since the file did not exist, it was directed to the index.php that was loaded one more time.

To be really sure, I renamed my style.css file to a nonexistent name. Page styles were not applied, Chrome's Inspection returned no 404 error, and in the file response was the full index.php code.

To solve the problem, I added a RewriteCond, verifying that the file was not the favicon.ico

Options -Indexes

<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !favicon.ico

    RewriteRule ^(.*)$ index.php?_url=$1 [L]

</IfModule>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.