This article illustrates the method of yii+mysql locking table to prevent duplication of data in case of concurrency. Share to everyone for your reference, specific as follows:
Lock Table Read lock
If a thread obtains a read lock on a table, the thread and all other threads can only read data from the table, and cannot do any write operations.
Lock tables User read;//read lock table
unlock tables;//unlock
lock tables User Read local;//Local read lock table, other thread inserts are not blocked, The update operation is blocked
Lock Table Write lock
If a thread gets a write lock on a table, only the thread that owns the lock can read and write the table from the table. The other threads are blocked.
Lock tables user write;//write lock table
unlock tables;//unlock
Examples of usages in Yii
/**
* Day single content status
/Public Function Getpointaready ($marke, $dayTime) {
$model = Sysrun::model ()-> Findbyattributes (Array (' syr_marking ' => $marke, ' syr_daytime ' => $dayTime));
if (empty ($model)) {
//table write lock
Yii::app ()->db->createcommand ()->settext ("Lock tables {Sys_run}}} WRITE ")->execute ();
$model = new Sysrun ();
$model->syr_marking = $marke;
$model->syr_daytime = $dayTime;
$model->syr_val = 0;
$model->syr_subval = 0;
$model->save ();
Table Unlock
Yii::app ()->db->createcommand ()->settext ("Unlock Tables")->execute ();
}
return $model;
}
For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to the YII framework and summary of common skills, the summary of PHP's excellent development framework, the Smarty Template Primer tutorial, and the summary of PHP Operations Office documentation (including Word,excel, ACCESS,PPT), "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on the YII framework.