douzhuang1900 2018-01-24 12:13
浏览 92

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

I separate the model queries on a trait class, because I hate to read a long block of codes with the model queries, and because I find it convenient if I reused the same function. But I found a problem once an error occur.

I was trying to run rollback function from eloquent once an error occur but, unfortunately, rollback wont work as I am expecting.

Am I doing it wrong? Are there any other ways to implement this?

    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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算