zoukankan      html  css  js  c++  java
  • php mvc 模式的开发注意事项

    1.控制器中:

    如果不涉及到数据库的就在控制器中。

    empty($res['code']) ? $this->error($res['msg']) : $this->success($res['msg']);  

    抛出异常写法: 

    try{
    $res = $Scoreflow->doaddscore($post);
    if(empty($res['code'])){
    throw new Exception($res['msg']);
    }
    $this->success($res['msg']);
    }catch(Exception $e){
    $this->error($e->getMessage());
    }

    2.模型中:

    所有关于数据库的操作都在模型里面做。

    return['code' => 0|1,'msg' => '返回信息'];//回复格式统一,会比较好处理

    模型中的回滚(需要用 self:: ,否则不起作用

    self::startTrans();
    try{
    //符合条件修改状态
    $exchangerecord_status = Exchangerecord::where('code',$code)
    ->update(['status' => '4']);
    //符合条件减库存
    $goods_stock = Goods::where('id',$exchangerecord['ngzb_goods_id'])
    ->setDec('stock', 1);
    // 提交事务
    self::commit();
    } catch (Exception $e) {
    // 回滚事务
    self::rollback();
    return['code'=>0,'msg'=>'兑换失败'];
    }
  • 相关阅读:
    短URL
    Linux安装MySQL
    Ubuntu中安装MySQL
    安装交叉工具链arm-linux-gcc
    Linux安装—IP设置
    Linux内核概述
    Bash变量
    Shell登陆
    Linux—查看远程Linux系统运行时间
    Linux—查看路由
  • 原文地址:https://www.cnblogs.com/roseY/p/11016399.html
Copyright © 2011-2022 走看看