对于由PHP函数分隔的查询,Laravel Eloquent回滚不起作用

时间:2018-01-24 12:13:22

标签: php eloquent laravel-5.4 laravel-eloquent

我在特征类上分离了模型查询,因为我讨厌用模型查询读取一长串代码,因为我觉得如果我重用相同的函数会很方便。但是一旦发生错误我就发现了问题。

一旦发生错误,我试图从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.');
    }

0 个答案:

没有答案