1<?PHP2 classIndexactionextendsAction {3 Private $d _user;4 Private $user;5 Private $arr;6 7 Public function__construct () {8Parent::__construct ();9 $this->d_user = d (' user ');Ten $this->user = M (' user '); One A //configuration information of the payment - $this->arr =Array( -' UserA ' = 1, the' UserB ' = 2, -' Money ' = 300 - ); - } + - /** + * Make money logic (transactional operation) A */ at Public functionindex () { - $this->user->Starttrans (); - $this->moneyfill ($this->user,$this->arr[' UserA '),$this->arr[' Money ']); - - $data=Array(' id ' = =$this->arr[' UserA ', ' money ' =Array(' exp ', ' money-'.)$this->arr[' Money '])); - $data 2=Array(' id ' = =$this->arr[' UserB ', ' money ' =Array(' exp ', ' money + '.$this->arr[' Money '])); in - if($data=$this->d_user->locktable ($data)){ to $res=$this->user->save ($data); + } - if($data 2=$this->d_user->locktable ($data 2)){ the $res 2=$this->user->save ($data 2); * } $ Panax Notoginseng if($res&&$res 2){ - $this->user->commit (); the Echo' Commit '; +}Else { A $this->user->rollback (); the Echo' Rollback '; + } - } $ $ /** - * Does the expense party's money meet - */ the Private functionMoneyfill ($user,$id,$money){ - $current _money=$user->where (Array(' id ' = =$id)->getfield (' money '));Wuyi if($current _money<$money){ the Echo' Money no worth! '; - Exit; Wu } - } About}
1<?PHP2 /**3 * User table Model class4 */5 classUsermodelextendsadvmodel{6 7 /**8 * Optimistic lock Operation9 */Ten Public functionLocktable ($res){ One A //Record optimistic lock - $res=$this->recordlockversion ($res); - the //Cache optimistic lock for current thread - $this->cachelockversion ($res); - - //Check optimistic lock and return lock + return $this->checklockversion ($res,$options); - } + } A?>
Thinkphp optimistic lock application after inheriting advanced model (in test)