Tp 5.0 中的事务操作详解


白俊遥博客

    /**
     * 订单添加
     * 含有关联添加和事务回滚
     * Power by Mikkle
     * QQ:776329498
     * @param $data
     * @return array
     */
    public function addOrder($data)
    {        if (!isset($data['consignee_id'])) return ['code' => '1003', 'msg' => '地址信息不存在'];
        $consignee = $this->table('my_user_address')->where(['guid' => $data['consignee_id'], 'status' => 1])->find();        if (!$consignee) return ['code' => '1003', 'msg' => '地址信息不存在'];
        $cart_list = json_decode($data['cart_list']);        if (!is_array($cart_list) || !$cart_list) return ['code' => '1003', 'msg' => '订单商品信息不存在'];        // 计算总金额
        $goods_money = 0;        foreach ($cart_list as $cart_key => $cart_value) {            if (is_object($cart_value)) {
                $goods = $this->table('v_guid_all')->where('my_guid', $cart_value->cart_id)->find();
                $goods_num = $cart_value->num;
            } elseif (is_array($cart_value)) {
                $goods = $this->table('my_guid_all')->where('my_guid', $cart_value['cart_id'])->find();
                $goods_num = $cart_value['num'];
            } else {
                $goods = [];
            }            if (!$goods) return ['code' => '1003', 'msg' => '商品信息不存在'];
            $data_order[$cart_key]['my_guid'] = $goods['my_guid'];
            $data_order[$cart_key]['my_price'] = $goods['my_price'];
            $data_order[$cart_key]['my_num'] = $goods_num;
            $goods_money = $goods_money + $goods['my_price'] * $goods_num;
        }
        $order_data['uuid'] = $data['uuid'];
        $order_data['amount'] = $goods_money;
        $order_data['order_num'] = count($data_order);
        $order_data['distribute'] = $data['distribute'];
        $order_data['order_state'] = 0;
        $order_data['expiration_time'] = time()+60*60*24*2;
        $order_data['order_desc'] = $data['remark'];
        $order_data['consignee_name'] = $consignee['consignee_name'];
        $order_data['consignee_mobile'] = $consignee['consignee_mobile'];
        $order_data['consignee_address'] = $consignee['province_name'] . ' ' . $consignee['area_name'] . ' ' . $consignee['district_name'] . ' ' . $consignee['consignee_address'];        try{            $this->startTrans();            $this->data($order_data)->isUpdate(false)->save();
            $new_order = $this->order_no;            if (!is_numeric($new_order)) throw new \Exception("商品订单添加失败");            $this->hasMany('OrdersAccess', 'order_no', 'order_no')->saveAll($data_order);            $this->commit();            return ['code' => '1001', 'msg' => '商品订单添加成功', 'data' => ['order_no' => $new_order, 'cart_list' => $data_order]];

        }catch (\Exception $e){            $this->rollback();            return ['code'=>'1008','msg'=>'商品订单添加失败','data'];
        }


    }



本文转载:https://www.kancloud.cn/mikkle/thinkphp5_study/329928


逆天的蝈蝈博客
请先登录后发表评论
  • 最新评论
  • 总共1条评论
逆天的蝈蝈博客

逆天的蝈蝈:事务处理,厉害了

2017-09-05 14:15:37 回复

逆天的蝈蝈博客
  • 逆天的蝈蝈 回复 逆天的蝈蝈逆天的蝈蝈博客
  • 2017-09-05 14:16:06 回复