yii的表关联&订单设计

in with 0 comment

yii笔记 - 唯一|文件|订单|beforeAction|hasMany

  1. 生成订单号 | 文件唯一

     // 生成一个唯一的订单号
     public function createNo($id)
     {
         $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
         $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
         return $orderSn;
     }
    
     -----------------------------
    
     function getFilename($file) {
         $array = ['jpg','gif','png'];
         $ext = (explode('.',$file))[1];
         if( !in_array($ext,$array) ) {
             throw new \Exception('文件类型错误');
         }
         $str1 = substr(md5($file . md5($file . random_int(10,10000)) ) . uniqid() , -10);
         $filename = date('Y-m_' , time()) . $str1;
         return $filename . '.' .$ext;
     }
    
  2. beforeAction()

    看了文档一下,这个实在是叙述的过于简单,而且按照文档是完全失效的

    请求处理过程是 应用层->控制器->init()->beforeAction->独立动作

    当 beforeAction 返回false的时候,后面的都将不再执行

    正常操作代码示例

     // 这是一个检查当前用户是否登录的控制器操作
    
     public function beforeAction($action)
     {
         $user = (new ServerController())->isLogin();
    
         if( is_null($user) ){
             return $this->redirect(['member/login'])->send();
         }
         $this->userid = $user['id'];
         return true;
     }
    
  3. hasMany 和 hasOne

     public function getProduct()
     {
         return $this->hasOne(Product::className(), ['id' => 'productid']);
     }
    
     public function getOderdetail()
     {
         return $this->hasMany(OrderDetail::className() , ['orderid' => 'id']);
     }
    
     // 先副表(就是准备关联的那个表)id 再主表 (当前模型表)id
    

订单的处理流程

首先添加商品到购物车 ->

在购物车页面将需要购买的商品入库 ->

使用事务,入库的同时将存在于购物车中的入库的商品进行删除

创建一个订单,已经一个订单详情数据表

检测一个订单的状态再进行后续操作

const CREATEORDER = 0;
const CHECKORDER = 100;
const PAYFAILED = 201;
const PAYSUCCESS = 202;
const SENDED = 220;
const RECEIVED = 260;

public static $status = [
    self::CREATEORDER => '订单初始化',
    self::CHECKORDER => '待支付',
    self::PAYFAILED => '支付失败',
    self::PAYSUCCESS => '支付成功',
    self::SENDED => '已发货',
    self::RECEIVED => '订单完成'
];

--------------------------------

 /**
 * 订单处理
 * $productids array ['productid' => 16 , 'productid'=>14]
 * $productnums array ['productnum' => 1 , 'productnum'=>3]
 * 分别互相对应
 * 通过遍历取出购物车中的数据,再将之插入订单详情表中
 * 删除购物车数据
 * 完成订单初始化
*/
public function orderHandler($data)
{
    $productids = $data['productid'];
    $productnums = $data['productnum'];
    $data = [];
    $transaction = \Yii::$app->db->beginTransaction();
        $orderid = $this->initOrder();  // 初始化一个新的订单
    try {
        foreach ($productids as $index => $productid) {   // 数据
            $product = Product::find()->where('id = :id' , [":id" => $productid])->one();
            if( !is_null($product) ) {
                $orderDetail = new OrderDetail();
                $data['productid'] = $product->id;
                $data['productnum'] = (int)$productnums[$index] ?? 1 ;
                $data['price'] = $product->saleprice;
                $data['createtime'] = time();
                $data['orderid'] = $orderid;
                if( !$orderDetail->add($data) ) {
                    throw new \Exception();
                }
                // 删除购物车中的数据
                $cart = Cart::find()->where("userid = $this->userid AND productid = $product->id")->one();
                $cart->delete();
           }
        }
        $transaction->commit();
        return $this->redirect(['order/index' , 'orderid'=> $orderid]);
    }catch (\Exception $e) {
        $transaction->rollBack();
        dd($e->getMessage());
        return $this->redirect(['cart/index']);
    }
}

// 初始化
public function initOrder()
{
    try{
        $order = new Order();
        $order->userid = $this->userid;
        $order->tradeno = $this->createNo($this->userid);
        $order->status = Order::CREATEORDER;
        $order->createtime = time();
        $order->updatetime = time();
        $order->save();

        return $order->id;
    }catch (\Exception $e) {
        throw new \Exception($e->getMessage());
    }
}
Comments are closed.