首页 php教程 php手册 [PHP] 商品类型规格属性后台管理(代码流程备忘) - 陶士涵

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

May 20, 2016 am 10:14 AM

实现界面

 

涉及到四张表,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

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)