A
A
alex4answ2019-04-21 12:57:45
Yii
alex4answ, 2019-04-21 12:57:45

Why are 2 entries added to the database instead of 1?

Good afternoon, I can not catch a double entry in the database.

В контроллере:
VisitService::newVisit();

в Визит сервисе:
public static function newVisit($page_id){
            $model = new Statistics();
            $model->load(self::getPlaceholder($page_id), '');
            self::saveVisit($model);
    }
private static function saveVisit($model){
        if($model->validate() && $model->save()){
            \Yii::debug('сохранение визита'); // пытаюсь посмотреть сколько раз будет в логах
            return true;
        }
        return false;
    }

As a result, the newVisit method is called 1 time, in 1 place, in the logs "saving the visit" also 1 time.
In the request debugger, there is only 1 such entry, it also has Duplicated = 1:
INSERT INTO `statistics` (`page_id`, `type`, `ip`, `date`) VALUES (42, 0, '127.0.0.1', NOW())

Moreover, records are recorded in a row with a difference of 2 seconds, redirects and other things were not found.
Please tell me where to look and what can be in general?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2019-04-21
@VladimirAndreev

Does the table have a primary key?
in general, insert or update - is determined on the basis of null === $_oldAttributes
it is filled during insert if there is a PC in the table.
if there is no PC and it cannot be added, you need to define your own getIsNewRecord() for the model and store some flag as an additional condition.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question