[PHP] 商品类型规格属性后台管理(代码流程备忘)

实现界面

涉及到四张表,type(商品类型表),type_spec(商品类型规格关联表),attribute(商品属性表),attribute_value(商品属性值表)

新建基控制器BaseController.class.php,向上抽取出来的公用方法

BaseController.class.php

<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {
    protected $pageSize=1;
    /**
     * 获取分页对象
     * @param  [type] $count [description]
     * @return [type]        [description]
     */
    public function getPager($count){
        $pager=new \Common\Libs\MyPage($count,$this->pageSize);
        return $pager;
    }
}

定义基模型文件BaseModel.class.php,继承系统的Model类

BaseModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class BaseModel extends Model{
    /**
     * 获取分页数据
     * @param  [type] $pager     [description]
     * @param  array  $condition [description]
     * @param  string $order     [description]
     * @return [type]            [description]
     */
    public function getPagerResult($pager,$condition=array(),$order=""){
        if($pager==null){
            return;
        }
        return $this->where($condition)
                    ->limit($pager->firstRow.‘,‘.$pager->listRows)
                    ->order($order)
                    ->select();
    }
    /**
     * 获取条数
     * @return [type] [description]
     */
    public function getCount($condition=array()){
        return $this->where($condition)->count();
    }
    /**
     * 添加数据
     * @return [type] [description]
     */
    public function addItem($data){
        $msg=array();
        if(!$this->create($data)){
            $msg[‘msg‘]=$this->getError();
            $msg[‘status‘]=false;
        }else{
            $id=$this->add($data);
            if($id){
                $msg[‘status‘]=true;
                $msg[‘id‘]=$id;
            }else{
                $msg[‘status‘]=false;
                $msg[‘msg‘]=$this->getError();
            }
        }
        return $msg;
    }
    /**
     * 获取单条数据
     * @return [type] [description]
     */
    public function getItem($condition=array()){
        return $this->where($condition)->find();
    }
    /**
     * 获取所有数据
     * @return [type] [description]
     */
    public function getAllResult($condition=array()){
        return $this->where($condition)->select();
    }
    /**
     * 删除数据
     * @return [type] [description]
     */
    public function delItem($condition=array()){
        if(empty($condition)){
            return false;
        }
        return $this->where($condition)->delete();
    }
    /**
     * 编辑数据
     * @return [type] [description]
     */
    public function setItem($condition=array(),$data){
        if(empty($condition)){
            return false;
        }
        $msg=array();
        if(!$this->create($data)){
            $msg[‘msg‘]=$this->getError();
            $msg[‘status‘]=false;
        }else{
            $id=$this->where($condition)->save($data);
            $msg[‘status‘]=true;
            $msg[‘id‘]=$id;
        }
        return $msg;
    }
}

新建类型控制器文件TypeController.class.php

TypeController.class.php

<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 类型(规格,属性)
 */
class TypeController extends BaseController {
    private $typeModel;
    private $specModel;
    private $attrModel;
    private $typeId;
    public function __construct(){
        parent::__construct();
        $this->typeModel=D("Type");
        $this->specModel=D("Spec");
        $this->attrModel=D("Attr");
    }
    /**
     * 列表
     * @return [type] [description]
     */
    public function index(){
        $nums=$this->typeModel->getCount();
        $pager=$this->getPager($nums);
        $typeList=$this->typeModel->getPagerResult($pager,array(),"type_id desc");
        $pageHtml=$pager->show();

        $this->assign(‘typeList‘,$typeList);
        $this->assign(‘pageHtml‘,$pageHtml);
        $this->display();
    }
    /**
     * 添加类型(添加进4张表 type,type_spec,attribute,attribute_value)
     * @return [type] [description]
     */
    public function addType(){
        if(IS_POST){
            //添加类型表
            $res=$this->typeModel->addTypeItem($_POST);
            if($res[‘status‘]){
                $this->typeId=$res[‘id‘];
                //添加属性
                if($_POST[‘attr_value‘][0][‘name‘]){
                    $this->addAttrData($_POST[‘attr_value‘]);
                }
                $this->success("操作成功!");
            }else{
                $this->error($res[‘msg‘]);
            }
        }else{
            $specList=$this->specModel->select();
            $this->assign("specList",$specList);
            $this->display();
        }
    }
    /**
     * 添加属性
     * @param [type] $data [description]
     */
    private function addAttrData($data){
        foreach ($data as $key => $value) {
            $temp=array();
            $temp[‘attr_name‘]=$value[‘name‘];
            $temp[‘attr_values‘]=$value[‘value‘];
            $temp[‘type_id‘]=$this->typeId;
            $this->attrModel->addAttrItem($temp);
        }
        return true;
    }
    /**
     * 编辑属性
     * @param [type] $data [description]
     */
    private function setAttrData($data){
        foreach ($data as $key => $value) {
            $temp=array();
            $temp[‘attr_id‘]=$key;
            $temp[‘attr_name‘]=$value[‘name‘];
            $temp[‘attr_values‘]=$value[‘value‘];
            $temp[‘type_id‘]=$this->typeId;
            $this->attrModel->setAttrItem($temp);
            //添加属性
            if($key==‘new‘){
                $this->addAttrData($value);
                break;
            }
        }
        return true;
    }
    /**
     * 编辑类型(添加进4张表 type,type_spec,attribute,attribute_value)
     * @return [type] [description]
     */
    public function editType(){
        $typeId=intval($_GET[‘typeId‘]);

        if(IS_POST){
            $this->typeId=intval($_POST[‘type_id‘]);
            //编辑类型表
            $res=$this->typeModel->editTypeItem($_POST);
            if($res[‘status‘]){
                //编辑属性
                if(!empty($_POST[‘attr_value‘])){
                    $this->setAttrData($_POST[‘attr_value‘]);
                }
                $this->success("操作成功!",U("Type/editType",array(‘typeId‘=>$this->typeId)));
            }else{
                $this->error($res[‘msg‘]);
            }
        }else{
            $typeInfo=$this->typeModel->getItem(array(‘type_id‘=>$typeId));
            $this->assign("typeInfo",$typeInfo);

            $specList=$this->specModel->select();
            $this->assign("specList",$specList);

            $specTypeList=M("type_spec")->where(array(‘type_id‘=>$typeId))->getField("spec_id",true);
            $this->assign("specTypeList",$specTypeList);

            $attrList=$this->attrModel->getAllResult(array(‘type_id‘=>$typeId));
            $this->assign("attrList",$attrList);

            $this->display();
        }
    }
}

新建类型模型文件TypeModel.class.php

TypeModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class TypeModel extends BaseModel{
    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array(
        array(‘type_name‘,‘require‘,‘类型名称必须!‘)
    );
    /**
    * 添加类型
    */
    public function addTypeItem($data){
        $res=$this->addItem($data);

        //添加类型规格
        $typeSpecs=$data[‘spec_id‘];
        $typeSpecArray=array();
        foreach ($typeSpecs as $typeSpec) {
            $temp=array();
            $temp[‘type_id‘]=$res[‘id‘];
            $temp[‘spec_id‘]=$typeSpec;
            $typeSpecArray[]=$temp;
        }

        M("type_spec")->addAll($typeSpecArray);
        return $res;
    }
    /**
    * 编辑类型
    */
    public function editTypeItem($data){
        $res=$this->setItem(array(‘type_id‘=>$data[‘type_id‘]),$data);

        //编辑类型规格
        M("type_spec")->where(array(‘type_id‘=>$data[‘type_id‘]))->delete();
        $typeSpecs=$data[‘spec_id‘];
        $typeSpecArray=array();
        foreach ($typeSpecs as $typeSpec) {
            $temp=array();
            $temp[‘type_id‘]=$data[‘type_id‘];
            $temp[‘spec_id‘]=$typeSpec;
            $typeSpecArray[]=$temp;
        }

        M("type_spec")->addAll($typeSpecArray);

        return $res;
    }
}

新建规格模型文件SpecModel.class.php

SpecModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class SpecModel extends BaseModel{
    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array(
        array(‘spec_name‘,‘require‘,‘规格名称必须!‘),
        array(‘spec_sort‘,‘number‘,‘排序必须是数字!‘)
    );
}

新建属性模型文件AttrModel.class.php

AttrModel.class.php

<?php
namespace Common\Model;
use Think\Model;
class AttrModel extends BaseModel{
    protected $tableName="attribute";
    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array(
        array(‘attr_name‘,‘require‘,‘属性名称必须!‘),
        array(‘type_id‘,‘require‘,‘类型id必须!‘),
        array(‘attr_values‘,‘require‘,‘属性值必须!‘)
    );
    /**
     * 添加属性
     */
    public function addAttrItem($data){
        $res=$this->addItem($data);

        //添加属性值
        $attrValues=explode("|", $data[‘attr_values‘]);
        $attrValueArray=array();
        foreach ($attrValues as $attrValue) {
            $temp=array();
            $temp[‘attr_id‘]=$res[‘id‘];
            $temp[‘attr_value‘]=$attrValue;
            $attrValueArray[]=$temp;
        }

        M("attribute_value")->addAll($attrValueArray);
    }
    /**
     * 编辑属性
     */
    public function setAttrItem($data){
        $res=$this->setItem(array(‘attr_id‘=>$data[‘attr_id‘]),$data);

        //编辑属性值
        M("attribute_value")->where(array(‘attr_id‘=>$data[‘attr_id‘]))->delete();
        $attrValues=explode("|", $data[‘attr_values‘]);
        $attrValueArray=array();
        foreach ($attrValues as $attrValue) {
            if(!$attrValue){
                break;
            }
            $temp=array();
            $temp[‘attr_id‘]=$data[‘attr_id‘];
            $temp[‘attr_value‘]=$attrValue;
            $attrValueArray[]=$temp;
        }

        M("attribute_value")->addAll($attrValueArray);
    }
}
时间: 2024-08-06 11:27:08

[PHP] 商品类型规格属性后台管理(代码流程备忘)的相关文章

C#常用代码片段备忘

以下是从visual studio中整理出来的常用代码片段,以作备忘 快捷键: eh 用途: 类中事件实现函数模板 private void MyMethod(object sender, EventArgs e) { throw new NotImplementedException(); } 快捷键: xmethod 有4个 用途: 类中公有静态方法的函数模板 public static void MyMethod(this object value) { throw new NotImpl

代码整洁备忘(一)

无聊在看<代码整洁之道>,找到了一些自己以前没有注意的地方,在这里记录下来,备忘一下. 目前看完了第九章. 1.重复很多的代码是不好的,需要仔细考虑去掉无用的重复. 2.变量,函数,类等的命名要足够精确,精简&易于搜索. 3.函数尽可能的少用参数(3个以内),&不要向函数内传递bool,因为这明确的说明了这个函数干的不是一件事!函数的职责应该是单一的.函数应该尽可能的短小,过长的函数是不好的. 4.注释,能不用就不用,能少用就少用.能用好的名字说明的问题就不要用注释来说明.标记

github提交代码——步骤备忘

打开git客户端,cd到要上传的项目文件夹下.   输入git init命令,回车,用来在当前项目的目录中生成本地的git管理库,我们可以发现在当前目录下多了一个.git的隐藏文件夹.   输入git add . 命令,回车,将项目中所有的文件添加到仓库中.如果想添加特定文件,把.换成对应的文件名就行了. 注意add后面有一个点,若是提交单独的文件,则可输入文件名.   输入git commit -m "first commit" 命令,本次提交的注释,双引号中的字符自定义修改.  

定位代码【备忘二】

_asm    {        call l        l :        pop eax            mov eval, eax    }    wchar_t buf[MAXBYTE] = { 0 };    _ltow_s(eval, buf, 16);

linux 相关代码路径备忘。

https://github.com/torvalds/linux/blob/master/net/core/dev.c?utf8=%E2%9C%93#L7736昨天 11:35 https://github.com/torvalds/linux/blob/master/net/core/sock.c?utf8=?#L56110:27 https://github.com/torvalds/linux/blob/master/net/core/dst.c?utf8=%E2%9C%93#L1541

PHP.38-TP框架商城应用实例-后台15-商品属性与库存量1-不同商品(唯一属性、可选属性),属性类型

思路: 1.不同商品属于不同的类型,如:手机.服装.电脑等类型 2.不同的类型有不同的属性,其中分为唯一属性和可选属性,如服装:可选属性{尺寸:S,M,L--;颜色:白色,黑色--}唯一属性:材质 首先把类型与属性关联起来 1.建表 类型表{p39_type} drop table if exists p39_type; create table p39_type ( id mediumint unsigned not null auto_increment comment 'Id', type

在基础管理下添加一个商品类型维护的模块(7-31)

一    验证不能为空的操作: 在form中添加代码@NotEmpty(field="商品类型", message="{errors.required}") 在controller中对应不能为空的位置添加 if(results.hasErrors()) return "manager/commodityType/addCommodityType"; 3.不能为空的格式:<td style="background-color: #f

Linux 程序设计学习笔记----Linux下文件类型和属性管理

转载请注明出处:http://blog.csdn.net/suool/article/details/38318225 部分内容整理自网络,在此感谢各位大神. Linux文件类型和权限 数据表示 文件属性存储结构体Inode的成员变量i_mode存储着该文件的文件类型和权限信息.该变量为short int类型. 这个16位变量的各个位功能划分为: 第0-8位为权限位,为别对应拥有者(user),同组其他用户(group)和其他用户(other)的读R写W和执行X权限. 第9-11位是权限修饰位,

黑马程序员---OC---内存管理(在对象属性的- setter和- dealloc方法里面写内存管理代码)

------iOS培训.Java培训.Android培训, iOS学习型技术博客,期待与您交流------ 内存管理(在对象属性的- setter和- dealloc方法里面写内存管理代码) 内存管理范围: 任何继承自NSObject的对象:其他数据类型(int.char.double.float.struct.enum等)不需要内存管理 对象的引用计数器: 每个OC对象内部都有自己的int类型(占据4个字节)的引用计数器,表示“对象被引用的次数”. 1> 当使用alloc.new或者copy创