How does Yii prevent inaccurate update data caused by concurrency? This article mainly introduces Yii's simple solution to prevent concurrency from causing inaccurate update data. Friends in need can refer to it. I hope to be helpful.
Share a useful piece of code from Yii:
When you need to increment a certain field in the database, such as counting the number of queries per day, change request_count after each request +1,
If written like this:
$model->request_count++; $flag = $model->save();
It will be inaccurate when encountering concurrency. It can be changed to:
$flag = static::updateAll([ 'report_count' => new \yii\db\Expression("`request_count` + 1") ], [ 'id' => $model->id ]);
Open six processes at the same time for insertion, and each process will increase 100 times, the first method only increased to 587, and the second method increased to 600.
Related recommendations:
Detailed explanation of the simple extension class for batch inserting data in the Yii framework
Solve the problem that IN query can only query one query in Yii framework parameterized query
##Yii solve the problem of DeleteAll connection table deletion error reporting
The above is the detailed content of Detailed explanation of how Yii prevents inaccurate update data caused by concurrency. For more information, please follow other related articles on the PHP Chinese website!