/** * 存储或更新进度 * @param $userId * @param $testTypeId * @param $testLibraryId * @return bool * @throws Exception * @throws \Exception */ public static function saveOrUpdate($userId, $testTypeId, $testLibraryId) { //查询用户是否有记录,有则更新,无则插入 $currentTestLibrary = self::findByUserAndTestType($userId, $testTypeId); if ($currentTestLibrary) { //如果上次记录和这次一样则直接跳过,不然update出错 if ($testLibraryId == $currentTestLibrary->testLibraryId) { return true; } $currentTestLibrary->testLibraryId = $testLibraryId; if ($currentTestLibrary->update()) { return true; } else { throw new Exception("CurrentTestLibrary update wrong"); } } else { $currentTestLibrary = new CurrentTestLibrary(); $currentTestLibrary->userId = $userId; $currentTestLibrary->testLibraryId = $testLibraryId; $currentTestLibrary->testTypeId = $testTypeId; if ($currentTestLibrary->save()) { return true; } else { throw new Exception("CurrentTestLibrary save wrong"); } } }