Search code examples
phpmysqlyiiyii2yii2-model

The "save" method of model class works very long


I have such method in controller, that simple sets value of status field to 0 or 1.

public function actionNews_status($id,$status){
    $status = ($status==1)?1:0;
    $number = false;
    $news = News::findOne($id);
    if(!is_null($news)){
        $news->status = $status;
        $number = $news->save();
        Redis::rset($CACHE_mat, null);
    }
    return json_encode(['status'=>$number]);
}

As it turned out, the method works for about 20 seconds.

I found out that line $number = $news->save(); is working slowly. I checked this with exit before and after this line. I also found out that the status field itself is updated immediately, since the query to the table instantly shows the changes. But the save method continues after that to work for a long time.

status field defined as TINYINT(1) NULL DEFAULT 0 and no have any indexes.

Maybe creation of index is to helps fix it?

MySQL database, InnoDB. In the table 78701 rows.

Updated:

I found out the following. Slowly running the afterSave method call at the end of the updateInternal. But before the line with this call, the script is executed quickly. Slowly works a call the afterSave by itself, not a code inside afterSave method and not code before afterSave calling Why this can happen?


Solution

  • Problem solved. Cause was found in overrided afterSave method in model class.