實現界面 涉及到四張表,type(商品類型表),type_spec(商品類型規格關聯表),attribute(商品屬性表),attribute_value(商品屬性值表) 新建基控制器BaseController.class.php,向上抽取出來的公用方法 BaseController.class. ...
實現界面
涉及到四張表,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); } }