[PHP] 商品类型规格属性后台管理(代码流程备忘) - 陶士涵
实现界面
涉及到四张表,type(商品类型表),type_spec(商品类型规格关联表),attribute(商品属性表),attribute_value(商品属性值表)
新建基控制器BaseController.class.php,向上抽取出来的公用方法
BaseController.class.php
<span style="color: #000000;">php namespace Admin\Controller; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Controller; </span><span style="color: #0000ff;">class</span> BaseController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Controller { </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$pageSize</span>=1<span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取分页对象 * @param [type] $count [description] * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getPager(<span style="color: #800080;">$count</span><span style="color: #000000;">){ </span><span style="color: #800080;">$pager</span>=<span style="color: #0000ff;">new</span> \Common\Libs\MyPage(<span style="color: #800080;">$count</span>,<span style="color: #800080;">$this</span>-><span style="color: #000000;">pageSize); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$pager</span><span style="color: #000000;">; } }</span>
로그인 후 복사
定义基模型文件BaseModel.class.php,继承系统的Model类
BaseModel.class.php
<span style="color: #000000;">php namespace Common\Model; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Model; </span><span style="color: #0000ff;">class</span> BaseModel <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Model{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取分页数据 * @param [type] $pager [description] * @param array $condition [description] * @param string $order [description] * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getPagerResult(<span style="color: #800080;">$pager</span>,<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span>(),<span style="color: #800080;">$order</span>=""<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$pager</span>==<span style="color: #0000ff;">null</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->where(<span style="color: #800080;">$condition</span><span style="color: #000000;">) </span>->limit(<span style="color: #800080;">$pager</span>->firstRow.','.<span style="color: #800080;">$pager</span>-><span style="color: #000000;">listRows) </span>->order(<span style="color: #800080;">$order</span><span style="color: #000000;">) </span>-><span style="color: #000000;">select(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取条数 * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getCount(<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">()){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->where(<span style="color: #800080;">$condition</span>)-><span style="color: #008080;">count</span><span style="color: #000000;">(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 添加数据 * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> addItem(<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #800080;">$msg</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>->create(<span style="color: #800080;">$data</span><span style="color: #000000;">)){ </span><span style="color: #800080;">$msg</span>['msg']=<span style="color: #800080;">$this</span>-><span style="color: #000000;">getError(); </span><span style="color: #800080;">$msg</span>['status']=<span style="color: #0000ff;">false</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$id</span>=<span style="color: #800080;">$this</span>->add(<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$id</span><span style="color: #000000;">){ </span><span style="color: #800080;">$msg</span>['status']=<span style="color: #0000ff;">true</span><span style="color: #000000;">; </span><span style="color: #800080;">$msg</span>['id']=<span style="color: #800080;">$id</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$msg</span>['status']=<span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #800080;">$msg</span>['msg']=<span style="color: #800080;">$this</span>-><span style="color: #000000;">getError(); } } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$msg</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取单条数据 * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getItem(<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">()){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->where(<span style="color: #800080;">$condition</span>)-><span style="color: #000000;">find(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取所有数据 * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getAllResult(<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">()){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->where(<span style="color: #800080;">$condition</span>)-><span style="color: #000000;">select(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 删除数据 * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> delItem(<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">()){ </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$condition</span><span style="color: #000000;">)){ </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->where(<span style="color: #800080;">$condition</span>)-><span style="color: #000000;">delete(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 编辑数据 * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setItem(<span style="color: #800080;">$condition</span>=<span style="color: #0000ff;">array</span>(),<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$condition</span><span style="color: #000000;">)){ </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } </span><span style="color: #800080;">$msg</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>->create(<span style="color: #800080;">$data</span><span style="color: #000000;">)){ </span><span style="color: #800080;">$msg</span>['msg']=<span style="color: #800080;">$this</span>-><span style="color: #000000;">getError(); </span><span style="color: #800080;">$msg</span>['status']=<span style="color: #0000ff;">false</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$id</span>=<span style="color: #800080;">$this</span>->where(<span style="color: #800080;">$condition</span>)->save(<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #800080;">$msg</span>['status']=<span style="color: #0000ff;">true</span><span style="color: #000000;">; </span><span style="color: #800080;">$msg</span>['id']=<span style="color: #800080;">$id</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$msg</span><span style="color: #000000;">; } }</span>
로그인 후 복사
新建类型控制器文件TypeController.class.php
TypeController.class.php
<span style="color: #000000;">php namespace Admin\Controller; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Controller; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 类型(规格,属性) </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">class</span> TypeController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseController { </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$typeModel</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$specModel</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$attrModel</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$typeId</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct(){ parent</span>::<span style="color: #000000;">__construct(); </span><span style="color: #800080;">$this</span>->typeModel=D("Type"<span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->specModel=D("Spec"<span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->attrModel=D("Attr"<span style="color: #000000;">); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 列表 * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> index(){ </span><span style="color: #800080;">$nums</span>=<span style="color: #800080;">$this</span>->typeModel-><span style="color: #000000;">getCount(); </span><span style="color: #800080;">$pager</span>=<span style="color: #800080;">$this</span>->getPager(<span style="color: #800080;">$nums</span><span style="color: #000000;">); </span><span style="color: #800080;">$typeList</span>=<span style="color: #800080;">$this</span>->typeModel->getPagerResult(<span style="color: #800080;">$pager</span>,<span style="color: #0000ff;">array</span>(),"type_id desc"<span style="color: #000000;">); </span><span style="color: #800080;">$pageHtml</span>=<span style="color: #800080;">$pager</span>-><span style="color: #000000;">show(); </span><span style="color: #800080;">$this</span>->assign('typeList',<span style="color: #800080;">$typeList</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->assign('pageHtml',<span style="color: #800080;">$pageHtml</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">display(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 添加类型(添加进4张表 type,type_spec,attribute,attribute_value) * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> addType(){ </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(IS_POST){ </span><span style="color: #008000;">//</span><span style="color: #008000;">添加类型表</span> <span style="color: #800080;">$res</span>=<span style="color: #800080;">$this</span>->typeModel->addTypeItem(<span style="color: #800080;">$_POST</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$res</span>['status'<span style="color: #000000;">]){ </span><span style="color: #800080;">$this</span>->typeId=<span style="color: #800080;">$res</span>['id'<span style="color: #000000;">]; </span><span style="color: #008000;">//</span><span style="color: #008000;">添加属性</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_POST</span>['attr_value'][0]['name'<span style="color: #000000;">]){ </span><span style="color: #800080;">$this</span>->addAttrData(<span style="color: #800080;">$_POST</span>['attr_value'<span style="color: #000000;">]); } </span><span style="color: #800080;">$this</span>->success("操作成功!"<span style="color: #000000;">); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$this</span>->error(<span style="color: #800080;">$res</span>['msg'<span style="color: #000000;">]); } }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$specList</span>=<span style="color: #800080;">$this</span>->specModel-><span style="color: #000000;">select(); </span><span style="color: #800080;">$this</span>->assign("specList",<span style="color: #800080;">$specList</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">display(); } } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 添加属性 * @param [type] $data [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> addAttrData(<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$data</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #800080;">$temp</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$temp</span>['attr_name']=<span style="color: #800080;">$value</span>['name'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['attr_values']=<span style="color: #800080;">$value</span>['value'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['type_id']=<span style="color: #800080;">$this</span>-><span style="color: #000000;">typeId; </span><span style="color: #800080;">$this</span>->attrModel->addAttrItem(<span style="color: #800080;">$temp</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 编辑属性 * @param [type] $data [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> setAttrData(<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$data</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #800080;">$temp</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$temp</span>['attr_id']=<span style="color: #800080;">$key</span><span style="color: #000000;">; </span><span style="color: #800080;">$temp</span>['attr_name']=<span style="color: #800080;">$value</span>['name'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['attr_values']=<span style="color: #800080;">$value</span>['value'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['type_id']=<span style="color: #800080;">$this</span>-><span style="color: #000000;">typeId; </span><span style="color: #800080;">$this</span>->attrModel->setAttrItem(<span style="color: #800080;">$temp</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">添加属性</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$key</span>=='new'<span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->addAttrData(<span style="color: #800080;">$value</span><span style="color: #000000;">); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 编辑类型(添加进4张表 type,type_spec,attribute,attribute_value) * @return [type] [description] </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> editType(){ </span><span style="color: #800080;">$typeId</span>=<span style="color: #008080;">intval</span>(<span style="color: #800080;">$_GET</span>['typeId'<span style="color: #000000;">]); </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(IS_POST){ </span><span style="color: #800080;">$this</span>->typeId=<span style="color: #008080;">intval</span>(<span style="color: #800080;">$_POST</span>['type_id'<span style="color: #000000;">]); </span><span style="color: #008000;">//</span><span style="color: #008000;">编辑类型表</span> <span style="color: #800080;">$res</span>=<span style="color: #800080;">$this</span>->typeModel->editTypeItem(<span style="color: #800080;">$_POST</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$res</span>['status'<span style="color: #000000;">]){ </span><span style="color: #008000;">//</span><span style="color: #008000;">编辑属性</span> <span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_POST</span>['attr_value'<span style="color: #000000;">])){ </span><span style="color: #800080;">$this</span>->setAttrData(<span style="color: #800080;">$_POST</span>['attr_value'<span style="color: #000000;">]); } </span><span style="color: #800080;">$this</span>->success("操作成功!",U("Type/editType",<span style="color: #0000ff;">array</span>('typeId'=><span style="color: #800080;">$this</span>-><span style="color: #000000;">typeId))); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$this</span>->error(<span style="color: #800080;">$res</span>['msg'<span style="color: #000000;">]); } }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$typeInfo</span>=<span style="color: #800080;">$this</span>->typeModel->getItem(<span style="color: #0000ff;">array</span>('type_id'=><span style="color: #800080;">$typeId</span><span style="color: #000000;">)); </span><span style="color: #800080;">$this</span>->assign("typeInfo",<span style="color: #800080;">$typeInfo</span><span style="color: #000000;">); </span><span style="color: #800080;">$specList</span>=<span style="color: #800080;">$this</span>->specModel-><span style="color: #000000;">select(); </span><span style="color: #800080;">$this</span>->assign("specList",<span style="color: #800080;">$specList</span><span style="color: #000000;">); </span><span style="color: #800080;">$specTypeList</span>=M("type_spec")->where(<span style="color: #0000ff;">array</span>('type_id'=><span style="color: #800080;">$typeId</span>))->getField("spec_id",<span style="color: #0000ff;">true</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->assign("specTypeList",<span style="color: #800080;">$specTypeList</span><span style="color: #000000;">); </span><span style="color: #800080;">$attrList</span>=<span style="color: #800080;">$this</span>->attrModel->getAllResult(<span style="color: #0000ff;">array</span>('type_id'=><span style="color: #800080;">$typeId</span><span style="color: #000000;">)); </span><span style="color: #800080;">$this</span>->assign("attrList",<span style="color: #800080;">$attrList</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">display(); } } }</span>
로그인 후 복사
新建类型模型文件TypeModel.class.php
TypeModel.class.php
<span style="color: #000000;">php namespace Common\Model; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Model; </span><span style="color: #0000ff;">class</span> TypeModel <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseModel{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 验证规则 * @var array </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_validate</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span><span style="color: #0000ff;">array</span>('type_name','require','类型名称必须!'<span style="color: #000000;">) ); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 添加类型 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> addTypeItem(<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #800080;">$res</span>=<span style="color: #800080;">$this</span>->addItem(<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">添加类型规格</span> <span style="color: #800080;">$typeSpecs</span>=<span style="color: #800080;">$data</span>['spec_id'<span style="color: #000000;">]; </span><span style="color: #800080;">$typeSpecArray</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$typeSpecs</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$typeSpec</span><span style="color: #000000;">) { </span><span style="color: #800080;">$temp</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$temp</span>['type_id']=<span style="color: #800080;">$res</span>['id'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['spec_id']=<span style="color: #800080;">$typeSpec</span><span style="color: #000000;">; </span><span style="color: #800080;">$typeSpecArray</span>[]=<span style="color: #800080;">$temp</span><span style="color: #000000;">; } M(</span>"type_spec")->addAll(<span style="color: #800080;">$typeSpecArray</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$res</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 编辑类型 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> editTypeItem(<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #800080;">$res</span>=<span style="color: #800080;">$this</span>->setItem(<span style="color: #0000ff;">array</span>('type_id'=><span style="color: #800080;">$data</span>['type_id']),<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">编辑类型规格</span> M("type_spec")->where(<span style="color: #0000ff;">array</span>('type_id'=><span style="color: #800080;">$data</span>['type_id']))-><span style="color: #000000;">delete(); </span><span style="color: #800080;">$typeSpecs</span>=<span style="color: #800080;">$data</span>['spec_id'<span style="color: #000000;">]; </span><span style="color: #800080;">$typeSpecArray</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$typeSpecs</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$typeSpec</span><span style="color: #000000;">) { </span><span style="color: #800080;">$temp</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$temp</span>['type_id']=<span style="color: #800080;">$data</span>['type_id'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['spec_id']=<span style="color: #800080;">$typeSpec</span><span style="color: #000000;">; </span><span style="color: #800080;">$typeSpecArray</span>[]=<span style="color: #800080;">$temp</span><span style="color: #000000;">; } M(</span>"type_spec")->addAll(<span style="color: #800080;">$typeSpecArray</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$res</span><span style="color: #000000;">; } }</span>
로그인 후 복사
新建规格模型文件SpecModel.class.php
SpecModel.class.php
<span style="color: #000000;">php namespace Common\Model; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Model; </span><span style="color: #0000ff;">class</span> SpecModel <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseModel{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 验证规则 * @var array </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_validate</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span><span style="color: #0000ff;">array</span>('spec_name','require','规格名称必须!'), <span style="color: #0000ff;">array</span>('spec_sort','number','排序必须是数字!'<span style="color: #000000;">) ); }</span>
로그인 후 복사
新建属性模型文件AttrModel.class.php
AttrModel.class.php
<span style="color: #000000;">php namespace Common\Model; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Model; </span><span style="color: #0000ff;">class</span> AttrModel <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseModel{ </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$tableName</span>="attribute"<span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 验证规则 * @var array </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_validate</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span><span style="color: #0000ff;">array</span>('attr_name','require','属性名称必须!'), <span style="color: #0000ff;">array</span>('type_id','require','类型id必须!'), <span style="color: #0000ff;">array</span>('attr_values','require','属性值必须!'<span style="color: #000000;">) ); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 添加属性 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> addAttrItem(<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #800080;">$res</span>=<span style="color: #800080;">$this</span>->addItem(<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">添加属性值</span> <span style="color: #800080;">$attrValues</span>=<span style="color: #008080;">explode</span>("|", <span style="color: #800080;">$data</span>['attr_values'<span style="color: #000000;">]); </span><span style="color: #800080;">$attrValueArray</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$attrValues</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$attrValue</span><span style="color: #000000;">) { </span><span style="color: #800080;">$temp</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$temp</span>['attr_id']=<span style="color: #800080;">$res</span>['id'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['attr_value']=<span style="color: #800080;">$attrValue</span><span style="color: #000000;">; </span><span style="color: #800080;">$attrValueArray</span>[]=<span style="color: #800080;">$temp</span><span style="color: #000000;">; } M(</span>"attribute_value")->addAll(<span style="color: #800080;">$attrValueArray</span><span style="color: #000000;">); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 编辑属性 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setAttrItem(<span style="color: #800080;">$data</span><span style="color: #000000;">){ </span><span style="color: #800080;">$res</span>=<span style="color: #800080;">$this</span>->setItem(<span style="color: #0000ff;">array</span>('attr_id'=><span style="color: #800080;">$data</span>['attr_id']),<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">编辑属性值</span> M("attribute_value")->where(<span style="color: #0000ff;">array</span>('attr_id'=><span style="color: #800080;">$data</span>['attr_id']))-><span style="color: #000000;">delete(); </span><span style="color: #800080;">$attrValues</span>=<span style="color: #008080;">explode</span>("|", <span style="color: #800080;">$data</span>['attr_values'<span style="color: #000000;">]); </span><span style="color: #800080;">$attrValueArray</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$attrValues</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$attrValue</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$attrValue</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; } </span><span style="color: #800080;">$temp</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$temp</span>['attr_id']=<span style="color: #800080;">$data</span>['attr_id'<span style="color: #000000;">]; </span><span style="color: #800080;">$temp</span>['attr_value']=<span style="color: #800080;">$attrValue</span><span style="color: #000000;">; </span><span style="color: #800080;">$attrValueArray</span>[]=<span style="color: #800080;">$temp</span><span style="color: #000000;">; } M(</span>"attribute_value")->addAll(<span style="color: #800080;">$attrValueArray</span><span style="color: #000000;">); } }</span>
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
1 몇 달 전
By DDD
R.E.P.O. 최고의 그래픽 설정
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
1 몇 주 전
By DDD
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7401
15


자바 튜토리얼
1630
14


Cakephp 튜토리얼
1358
52


라라벨 튜토리얼
1268
25


PHP 튜토리얼
1217
29

