tp5 商城商品模型删除

1:控制器代码

 public function delete($id)
    {
        //验证id是否正确  $id
        if (!intval($id)) {
            return getJsonData(10010, '参数不正确');
        }
        //删除操作
        //检测商品是否存在
        $where = [
            'type_id' => $id
        ];
        $goodsInfo = \app\admin\model\Goods::getGoodsInfo($where);
        if (!empty($goodsInfo)) {
            return getJsonData(10010, '商品正在使用不能删除');
        }
        // 启动事务
        Db::startTrans();
        try { //try  catch  php 捕捉异常
            //删除商品模型数据
            typeModel::deleteTypeInfo($id);
            //删除商品属性
            Attribute::deleteAttrInfo($where);
            //删除商品规格
            Spec::deleteSpecInfo($where);
            //删除商品属性
            SpecValue::deleteSpecValueInfo($where);

            // 提交事务
            Db::commit();
            return getJsonData(200, '删除成功');
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return getJsonData(100010, $e->getMessage());
        }
    }

2:商品模型数据

  //删除商品模型
    public static function deleteTypeInfo($id)
    {
        return self::destroy($id);
    }

3:删除商品属性

?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Attribute extends Model
{
    //
    use SoftDelete;


    //删除商品属性
    public static function deleteAttrInfo($id)
    {
        return self::destroy($id);
    }
}

4:删除商品规格

<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Spec extends Model
{
    use SoftDelete;

    protected $table = 'pyg_spec';

    //定义商品模型和商品规格关系 一对多
    public function specValues()
    {
        return $this->hasMany('SpecValue', 'spec_id','id');
    }


    //删除商品规格
    public static function deleteSpecInfo($id)
    {
        return self::destroy($id);
    }
}

5,删除商品规格值:

<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class SpecValue extends Model
{
    //
    use SoftDelete;

    //删除商品规格值
    public static function deleteSpecValueInfo($id)
    {
        return self::destroy($id);
    }
}

 

 

posted @ 2021-09-10 20:36  王越666  阅读(61)  评论(0)    收藏  举报