我在特征类上分离了模型查询,因为我讨厌用模型查询读取一长串代码,因为我觉得如果我重用相同的函数会很方便。但是一旦发生错误我就发现了问题。
一旦发生错误,我试图从eloquent运行回滚功能但不幸的是,回滚不会像我期望的那样工作。
我做错了吗? 还有其他方法可以实现吗?
try {
DB::beginTransaction();
// UserDetails
$userdetailsID = $this->saveUserDetails($request,$userData->id);
if($userdetailsID){
$result = $this->saveUser(
$request,
$this->getHashValue($request->password),
$userdetailsID,
$userData->id,
$this->cleanTobeSafeAsDirectory(crypt(($userdetailsID.$userData->companyid), 'rl'))
);
if($result){
$updalodResult = $this->uploadThisImage( $request , 'images/uploads/users/'.$userdetailsID.'/icon/', '_'.$this->getHashValue($userdetailsID),'userImageAvatar');
if($updalodResult['success']){
$resul = $this->getThisUserDetials($userdetailsID);
$resul->photo_name = $updalodResult['filename'];
$resul->save();
$imageFilePath = 'images/uploads/users/'.$userdetailsID.'/icon/'.$updalodResult['filename'];
$this->cropImageJpegOnly($imageFilePath,$request->img_x,$request->img_y,$request->img_w,$request->img_h,$request->img_width,$request->img_height);
}
DB::commit();
return $this->returnAsAppSuccess('User information added.');
}
DB::rollBack();
return $this->returnAsAppError('Failed to user security details.' );
}
DB::rollBack();
return $this->returnAsAppError('Failed to user security details.' );
} catch (PDOException $e) {
DB::rollBack();
return $this->returnAsAppError('Failed to insert User information.');
}