In-depth analysis of ThinkPHP Framework Association Model implementation (how to ensure transaction consistency using PHP) background: ThinkPHP version: 3.0 when the Mysql table engine does not support transactions, how can we ensure the consistency of more than two data transactions? ThinkPHP has an associated model, so I have thoroughly studied the environment: two in-depth analysis of the ThinkPHP framework associated model implementation (how to ensure transaction consistency using PHP)
Background:
ThinkPHP version: 3.0
When the Mysql table engine does not support transactions, how can we ensure the consistency of more than two data transactions?
ThinkPHP has an associated model. Therefore, I have studied it in depth.
Environment: two table User tables :'
UserRole table
The user_id in the userRole table is a very simple table with the master id of the User table. then configure UserModel. class. php.
All configured. Next, write the action method.
public function add(){$data['username']='zpass';$data['password']='password';$data['email']='email';$rs=D('User')->relation(true)->add($data);dump($rs);}
Start to execute the experiment and start to execute the add method. search under relation. the add method is available. but there is a _ call method. but it will not be executed, because the add method exists and inherits the Model. class. php, so the add method is very simple. you must first execute the Model. class. the user table of the php method is inserted. Next, the _ after_insert method is used in Moel. class. php is empty, do nothing but in relationModel. class. php is overloaded, so the relationModel is executed. class. the _ after_insert method in php. this method is used to determine whether there is a defined association model. if there is a definition, it is executed and then inserted into the userRole table. conclusion The association model of ThinkPHP cannot guarantee transaction consistency between the two tables. I tried it. when the userRole table is not filled in, only the User table data is inserted. if the userRole table does not exist, you can only ensure that the user table does not exist ., the userRole table does not exist either. there are actually no methods for PHP to implement Mysql-like transactions, or I haven't found them yet. A good method is to delete the first SQL statement when the execution of the second SQL statement fails.