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

*文
Release: 2023-03-19 07:40:01
Original
1473 people have browsed it

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();
Copy after login

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
]);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!