laravel的事务使用如下:
DB::connection('gvideo')->transaction(function () use ($user_id, $video_id, $action_id) { $this->create(array('user_id' => $user_id, 'video_id' => $video_id, 'action_id' => $action_id)); if (UserTotalActionRecord::where('user_id', $user_id)->where('action_id', $action_id)->where('record_date', date("Y-m-d"))->first()) { UserTotalActionRecord::where('user_id', $user_id)->where('action_id', $action_id)->where('record_date', date("Y-m-d"))->increment('action_times'); } else { UserTotalActionRecord::create(array('user_id' => $user_id, 'action_id' => $action_id, 'record_date' => date("Y-m-d"))); } });
之前使用时发现匿名函数没用 use 来引用外部变量,使得在函数体内使用时会出错,因此需注重匿名函数的使用。