PHP.36-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除修改

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP.36-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除修改相关的知识,希望对你有一定的参考价值。

商品分类删除

1、删除商品时,根据商品id删除扩展分类表数据

 

商品扩展分类修改

1、在控制器GoodsController.class.php/edit()中根据商品id取出对应的所有扩展分类

2、在修改页面edit.html中显示对应扩展分类foreach,类似添加页面

  注:考虑当没有扩展分类时的显示,使用if($gcData)

3、在模型类GoodsModel.class.php/_before_update()中处理扩展分类,先删除原数据,再插入新数据【多对多时,通常的用法】

 

<?php
    namespace Admin\\Model;
    use Think\\Model;
    
    class GoodsModel extends Model
    {    
        //添加调用create方法允许接收的字段
        protected $insertFields = \'goods_name,market_price,shop_price,is_on_sale,goods_desc,brand_id,cat_id\';
        //修改调用create方法允许接收的字段
        protected $updateFields = \'id,goods_name,market_price,shop_price,is_on_sale,goods_desc,brand_id,cat_id\';
        
        
        //定义验证规则    validate:TP模型层提供的一种数据验证方法 
            //a.静态方式:在模型类里面通过$_validate属性定义验证规则。b.动态方式:使用模型类的validate方法动态创建自动验证规则 
            //定义好验证规则后,就可以在使用create方法创建数据对象的时候自动调用
        protected $_validate = array(
            array(\'cat_id\', \'require\', \'必须选择主分类\', \'1\'),
            array(\'goods_name\', \'require\', \'商品名称不能为空!\', \'1\'),
            array(\'market_price\', \'currency\', \'市场价格必须是货币!\', \'1\'),
            array(\'shop_price\', \'currency\', \'本店价格必须是货币类型!\', \'1\'),
        );
        
        
        
        //钩子方法_before_insert:添加前插入,在添加前会自动调用
        //第一个参数:表单中即将要被插入数据库中的数据=>数组
        //&按引用传递:函数外部的变量的值要在函数内部修改的话,必须按引用传递,除非传递的为对象,因为对象默认按引用传递
        protected function _before_insert(&$data, $option)
        {
            $id = $option[\'where\'][\'id\'];    //要修改的商品的ID
            
            /**************处理LOGO******************/
            //判断有没有选择图片
            if($_FILES[\'logo\'][\'error\'] == 0)
            {
                $ret = uploadOne(\'logo\', \'Goods\', array(
                    array(700, 700),
                    array(350, 350),
                    array(130, 130),
                    array(50, 50),
                ));
                $data[\'logo\'] = $ret[\'images\'][0];
                $data[\'mbig_logo\'] = $ret[\'images\'][1];
                $data[\'big_logo\'] = $ret[\'images\'][2];
                $data[\'mid_logo\'] = $ret[\'images\'][3];
                $data[\'sm_logo\'] = $ret[\'images\'][4];
            }
            //获取当前时间并添加到表单中,这样就会插入数据库中
            $data[\'addtime\'] = date(\'Y-m-d H:i:s\', time());
            
            //过滤这个字段    【必须对所有输入内容进行过滤】
            $data[\'goods_desc\'] = removeXSS($_POST[\'goods_desc\']);
        }
        
        //钩子方法_before_update:更新前插入,在添加前会自动调用
        protected function _before_update(&$data, $option)
        {
            $id = $option[\'where\'][\'id\'];    //要修改的商品的ID
            /**********处理扩展分类【类似删除会员价格】*************/
            $ecid = I(\'post.ext_cat_id\');
            $gcModel = M(\'goods_cat\');
            //先删除原分类数据(根据商品id)
            $gcModel->where(array(
                \'goods_id\' => array(\'eq\', $id),
            ))->delete();
            if($ecid)
            {
                foreach ($ecid as $k => $v)
                {
                    if(empty($v))    //避免插入空数据
                        continue;
                    $gcModel->add(array(
                        \'cat_id\' => $v,
                        \'goods_id\' => $id,
                    ));
                }
            }
            
            /**************处理LOGO******************/
            //判断有没有选择图片
            if($_FILES[\'logo\'][\'error\'] == 0)
            {
                $ret = uploadOne(\'logo\', \'Goods\', array(
                    array(700, 700),
                    array(350, 350),
                    array(130, 130),
                    array(50, 50),
                ));
                $data[\'logo\'] = $ret[\'images\'][0];
                $data[\'mbig_logo\'] = $ret[\'images\'][1];
                $data[\'big_logo\'] = $ret[\'images\'][2];
                $data[\'mid_logo\'] = $ret[\'images\'][3];
                $data[\'sm_logo\'] = $ret[\'images\'][4];
                    
                    //先查询出原来的图片的路径
                $oldLogo = $this->field(\'logo,mbig_logo,big_logo,mid_logo,sm_logo\')->find($id);
                    //从硬盘上删除图片
                deleteImage($oldLogo);
            }
            
            /**********商品相册处理********/
            if(isset($_FILES[\'pic\']))    //先判断是否有上传
            {
                $pics = array();
                //var_dump($_FILES[\'pic\'][\'name\']);die();
                //二维数组转成一维
                foreach ($_FILES[\'pic\'][\'name\'] as $k => $v)
                {
                    $pics[] = array(
                        \'name\' => $v,
                        \'type\' => $_FILES[\'pic\'][\'type\'][$k],
                        \'tmp_name\' => $_FILES[\'pic\'][\'tmp_name\'][$k],
                        \'error\' => $_FILES[\'pic\'][\'error\'][$k],
                        \'size\' => $_FILES[\'pic\'][\'size\'][$k],
                    );
                }
                //var_dump($pics);die();
                $_FILES = $pics;    //把处理好的数组赋给$_FILES,因为uploadOne函数是在$_FILES中获取图片的
                $gpModel = M(\'goods_pic\');
                //循环每个上传
                foreach ($pics as $k => $v)
                {
                    if($v[\'error\'] == 0)
                    {
                        $ret = uploadOne($k, \'Goods\', array(
                            array(650, 650),
                            array(350, 350),
                            array(50, 50),
                        ));
                        //var_dump($ret);die();
                        if($ret[\'ok\'] == 1)
                        {
                            $gpModel->add(array(
                                \'pic\' => $ret[\'images\'][0],
                                \'big_pic\' => $ret[\'images\'][1],
                                \'mid_pic\' => $ret[\'images\'][2],
                                \'sm_pic\' => $ret[\'images\'][3],
                                \'goods_id\' => $id,
                            ));
                        }
                    }
                }
            }
            /************处理会员价格****************/
            $mp = I(\'post.member_price\');
            $mpModel = M(\'member_price\');
            //先删除原来的会员价格
            $mpModel->where(array(
                \'goods_id\' => array(\'eq\', $id),
            ))->delete();
            foreach ($mp as $k => $v)
            {
                $_v = (float)$v;
                //如果设置了会员价格就插入到表中
                if($_v > 0)
                {
                    $mpModel->add(array(
                        \'price\' => $_v,
                        \'level_id\' => $k,
                        \'goods_id\' => $id,
                    ));
                }
            }
                        
            //过滤这个字段    【必须对所有输入内容进行过滤】
            $data[\'goods_desc\'] = removeXSS($_POST[\'goods_desc\']);
        }
        
    
        //钩子方法_before_delete:删除前的操作
        protected function _before_delete($option)
        {
            $id = $option[\'where\'][\'id\'];    //要删除的商品的ID
            /**********删除扩展分类(根据商品id)*********/
            $gcModel = M(\'goods_cat\');
            $gcModel->where(array(
                \'goods_id\' => array(\'eq\', $id),
            ))->delete();
            
            /***********删除商品相片********/
            //先查询出原商品相册的路径
            $gpModel = M(\'goods_pic\');
            $pics = $gpModel->field(\'pic,sm_pic,mid_pic,big_pic\')->where(array(
                \'goods_id\' => array(\'eq\', $id),
            ))->select();
            foreach ($pics as $k => $v){
                deleteImage($v);        //$v是一个数组
            }
            $gpModel->where(array(        //根据商品id删除数据库中的路径信息
                \'goods_id\' => array(\'eq\', $id),
            ))->delete();
            
            /***********删除LOGO********/
            //先查询出原来的图片的路径
            $oldLogo = $this->field(\'logo,mbig_logo,big_logo,mid_logo,sm_logo\')->find($id);        
            deleteImage($oldLogo);
            /****** 删除会员价格 ********/
            //根据商品表id 删除操作【后用了外键级联删除,则不需要写这段代码】
            /* $mpModel = D(\'member_price\');
            $mpModel->where(array(
                \'goods_id\' => array(\'eq\', $id),
            ))->delete(); */
        }
        
        //钩子方法_after_insert:添加操作成功后执行
        protected function _after_insert($data, $option)
        {
            /***************处理扩展分类**************/
            $ecid = I(\'post.ext_cat_id\');
            if($ecid)
            {
                $gcModel = M(\'goods_cat\');
                foreach ($ecid as $k => $v)
                {
                    if(empty($v))
                        continue;
                    $gcModel->add(array(
                        \'cat_id\' => $v,
                        \'goods_id\' => $data[\'id\'],
                    ));
                }
            }
            
            /**********商品相册处理********/
            $pics = array();
            //var_dump($_FILES[\'pic\'][\'name\']);die();
            //二维数组转成一维
            foreach ($_FILES[\'pic\'][\'name\'] as $k => $v)
            {
                $pics[] = array(
                    \'name\' => $v,
                    \'type\' => $_FILES[\'pic\'][\'type\'][$k],
                    \'tmp_name\' => $_FILES[\'pic\'][\'tmp_name\'][$k],
                    \'error\' => $_FILES[\'pic\'][\'error\'][$k],
                    \'size\' => $_FILES[\'pic\'][\'size\'][$k],
                );
            }
            //var_dump($pics);die();
            $_FILES = $pics;    //把处理好的数组赋给$_FILES,因为uploadOne函数是在$_FILES中获取图片的
            $gpModel = M(\'goods_pic\');
            //循环每个上传
            foreach ($pics as $k => $v)
            {
                if($v[\'error\'] == 0)
                {
                    $ret = uploadOne($k, \'Goods\', array(
                        array(650, 650),
                        array(350, 350),
                        array(50, 50),
                    ));
                    //var_dump($ret);die();
                    if($ret[\'ok\'] == 1)
                    {
                        $gpModel->add(array(
                            \'pic\' => $ret[\'images\'][0],
                            \'big_pic\' => $ret[\'images\'][1],
                            \'mid_pic\' => $ret[\'images\'][2],
                            \'sm_pic\' => $ret[\'images\'][3],
                            \'goods_id\' => $data[\'id\'],
                        ));
                    }
                }
            }
            /**********会员价格处理********/
            $mp = I(\'post.member_price\');    //接收post提交过来的会员价格数据
            $mpModel = D(\'member_price\');
            foreach ($mp as $k => $v)
            {
                $_v = (float)$v;    //强制转为浮点型,以免插入字符等错误数据
                //设置会员价格>0就插入到表中
                if($_v > 0)
                {
                    $mpModel->add(array(
                        \'price\' => $_v,
                        \'level_id\' => $k,        //级别Id
                        \'goods_id\' => $data[\'id\'],
                    ));
                }
            }
        }
        
        /**
         *实现翻页、搜索、排序
         *
         */
        public function search($perPage = 5)    //$perPage控制显示条数
        {
            /***********搜索(获取get提交的数据)************/
            $where =array();    //空的where条件
                //商品名称
            $gn = I(\'get.gn\');
            if($gn)
                $where[\'a.goods_name\'] = array(\'like\', "%$gn%");    //WHERE goods_name LIKE \'%$gn%\'
                //品牌
            $brandId = I(\'get.brand_id\');
            if($brandId)
                $where[\'a.brand_id\'] = array(\'eq\', "$brandId");    //WHERE goods_name LIKE \'%$gn%\'
                /***主分类搜索***/
            $catId = I(\'get.cat_id\');
            if($catId)
            {
                //先查询出这个分类ID下所有的商品ID
                $gids = $this->getGoodsIdByCatId($catId);
                
                //搜索出所有这些id下的商品
                $where[\'a.id\'] = array(\'in\', $gids);
            }
                //市场价格
            $fp = I(\'get.fp\');
            $tp = I(\'get.tp\');
            if($fp && $tp)
                $where[\'a.shop_price\'] = array(\'between\', array($fp, $tp));    //WHERE shop_price BETWEEN $fp AND $tp
            elseif($fp)
                $where[\'a.shop_price\'] = array(\'egt\', $fp);    //WHERE shop_price >= $fp
            elseif($tp)
                $where[\'a.shop_price\'] = array(\'elt\', $tp);    //WHERE shop_price <= $tp
                //是否上架
            $ios = I(\'get.ios\');
            if($ios)
                $where[\'a.is_on_sale\'] = array(\'eq\', $ios);    //WHERE is_on_sale = $ios
                //添加时间
            $fa = I(\'get.fa\');
            $ta = I(\'get.ta\');
            if($fa && $ta)
                $where[\'a.addtime\'] = array(\'between\', array($fa, $ta));    //WHERE addtime BETWEEN $fa ADD $ta
            elseif($fa)
                $where[\'a.addtime\'] = array(\'egt\', $fa);    //WHERE addtime >= $fa
            elseif($ta)
                $where[\'a.addtime\'] = array(\'elt\', $ta);    //WHERE addtime <= $ta
            
            /***********翻页**********/
                //取出总的记录数
            $count = $this->alias(\'a\')->where($where)->count();
                //生成翻页类的对象
            $pageObj = new \\Think\\Page($count, $perPage);
                //设置样式
            $pageObj->setConfig(\'next\', \'下一页\');
            $pageObj->setConfig(\'prev\', \'上一页\');
                //生成页面下面显示的上一页、下一页的字符串
            $pageString = $pageObj->show();
            
            /**********排序********************/
            $orderby = \'a.id\';        //默认的排序字段
            $orderway = \'desc\';        //默认的排序方式(降序)
            $odby = I(\'get.odby\');
            if($odby)
            {
                if($odby == \'id_asc\')    //时间升序,id为自增
                    $orderway = \'asc\';
                elseif($odby == \'price_desc\')    //价格降序(默认降序)
                    $orderby = \'a.shop_price\';
                elseif($odby == \'price_asc\')    //价格升序
                {
                    $orderby = \'a.shop_price\';
                    $orderway = \'asc\';
                }
            }
            
            /**********取某一页的数据**********/
            /***
             * SELECT a.*,b.brand_name FROM p39_goods a LEFT JOIN p39_brand b ON a.brand_id=b.id;
             **/

            $data = $this->order("$orderby $orderway")
            ->field(\'a.*, b.brand_name, c.cat_name,GROUP_CONCAT(e.cat_name SEPARATOR "<br />") ext_cat_name\')
            ->alias(\'a\')                //加别名
            ->join(\'LEFT JOIN __BRAND__ b ON a.brand_id=b.id
                   LEFT JOIN __CATEGORY__ c ON a.cat_id=c.id
                   LEFT JOIN __GOODS_CAT__ d ON a.id=d.goods_id
                   LEFT JOIN __CATEGORY__ e ON d.cat_id=e.id\')
            ->where($where)                                            //搜索
            ->limit($pageObj->firstRow.\',\'.$pageObj->listRows)        //翻页
            ->group(\'a.id\')                //以商品id分组
            ->select();
            
            /************返回数据$data*************/
            return array(
                \'data\' => $data,    //数据
                \'page\' => $pageString,    //翻页字符串
            );
        }
        
        /**
         *    取出一个分类下所有商品的ID【既考虑主分类也考虑扩展分类】
         **/
        public function getGoodsIdByCatId($catId)
        {
            //先取出所有子分类的ID        【$catId=>传入是搜索的分类ID】
            $catModel = D(\'category\');
            $children = $catModel->getChildren($catId);
            //和子分类放一起            【子分类为扩展分类,也应该被搜索出来】
            $children[] = $catId;
            /********************取出主分类或者扩展分类在这下分类中的商品*******/
            //取出主分类下的商品Id
            $gids = $this->field(\'id\')->where(array(
                \'cat_id\' => array(\'in\', $children),        //主分类下的商品
            ))->select();
            //取出扩展分类下的商品ID
            $gcModel = M(\'goods_cat\');
            $gids1 = $gcModel->field(\'DISTINCT goods_id id\')->where(array(
                \'cat_id\' => array(\'IN\', $children)
            ))->select();
            //把主分类的ID和扩展分类下的商品ID合并成二维数据【两个都不为空】
            if($gids && $gids1)
                $gids = array_merge($gids, $gids1);        //合并数组
            elseif ($gids1)
                $gids = $gids1;
            //二维转一维
            $id = array();
            foreach ($gids as $k => $v)
            {
                if(!in_array($v[\'id\'], $id))
                    $id[] = $v[\'id\'];
            }
            return $id;
        }

    }
?>
GoodsModel.class.php

 

 

 

 

以上是关于PHP.36-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除修改的主要内容,如果未能解决你的问题,请参考以下文章

PHP.31-TP框架商城应用实例-后台9-商品相册-修改删除(AJAX)

PHP.30-TP框架商城应用实例-后台6-商品会员-价格级别

PHP.39-TP框架商城应用实例-后台16-商品属性2-AJAX添加删除

PHP.26-TP框架商城应用实例-后台2-商品列表页-搜索翻页排序

PHP.27-TP框架商城应用实例-后台3-商品修改删除

PHP.32-TP框架商城应用实例-后台10-商品分类-需求分析创建无限级商品分类,递归