Home  >  Article  >  Backend Development  >  Detailed explanation of how Yii prevents inaccurate update data caused by concurrency

Detailed explanation of how Yii prevents inaccurate update data caused by concurrency

*文
*文Original
2018-01-03 13:47:051378browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn