1 public static function alter($aid, $rid) 2 { 3 $incDec = GoldRule::where(['gold_rule_isdel'=>1,'gold_rule_status'=>2]) 4 ->where('gold_rule_id', $rid) 5 ->value('gold_rule_num'); 6 $isHas = self::where('gold_aid', $aid)->find(); 7 8 $model = new Gold(); 9 if ($isHas) { 10 try{ . 11 $ Model -> startTrans (); // open transaction 12 is $ Gold = $ Model -> Lock ( to true ) -> WHERE ( 'gold_aid', $ AID ) -> Find (); // lock 13 is $ RES = Gold $ -> WHERE ( 'gold_aid', $ AID ) -> update ([ 'gold_number' => $ isHas [ 'gold_number'] + $ incDec ]); // perform an update 14 // SLEEP (. 5); // You can use sleep () function to verify lock plus successful second update will first wait for the update to complete 15 $ Model -> the commit (); // transaction commits 16 return $ RES ; 17 }catch(\Exception $exception){ 18 $model->rollback(); 19 throw $exception; 20 } 21 } else { 22 23 $model->gold_aid = $aid; 24 $model->gold_number = $incDec; 25 return $model->save(); 26 } 27 }