[PHP] 商品类型规格属性后台管理(代码流程备忘) - 陶士涵
程序员文章站
2022-05-09 23:00:15
...
实现界面
涉及到四张表,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); } }