一 新建TypeController类,实现添加类型的功能
1 新建Admin/Controller/TypeController.class, 新建Admin/View/Type/add.html, 新建add方法载入页面
2 新建Common/Model/TypeModel.class继承BaseMdoel,声明主键ID与表名
3 在BaseModel中新建Store方法,判断编辑还是储存,然后返回1个数组,status状态和message回复消息
1 2 3 4 5 6 7 8 9 | public function store($data){ if ($ this ->create($data)){ $action = isset($data[$ this ->pk]) ? 'save' : 'add' ; $ this ->$action($data); return [ 'status' => 'success' , 'message' => '操作成功' ]; } else { return [ 'status' => 'failed' , 'message' =>$ this ->getError()]; } } |
4 在AdminController新建Store方法,传入2个形参,第一个参数为实例化的model名称
第二个参数为将要储存的数据,调用BaseModel中的store方法进行储存
1 2 3 4 | public function store(Model $model,$post){ $result = $model->store($post); $ this ->message($result); } |
5 在AdminController新建message方法,接收Store方法的参数,进行判断回复消息
1 2 3 4 5 6 7 | public function message($msg){ if ($msg[ 'status' ] == 'successs' ){ $ this ->success($msg[ 'message' ]); } else { $ this ->error($msg[ 'message' ]); } } |
6 在TypeController中IS_POST,进行判断是否为空,然后储存数据,调用store方法
1 2 3 4 5 6 7 8 9 | public function add(){ if (IS_POST){ if (!I( 'post.tname' )){ $ this ->error( '请输入类型名称' ); } $ this ->store( new TypeModel(),I( 'post.' )); } $ this ->display(); } |
二 TypeController建立方法,实现为类型的属性的增删改查
1.新建lists方法,新建View/Type/lists.html模板,载入类型列表页.
1 2 3 4 | public function lists(){ $oldData = M( 'type' )->select(); $ this ->assign( 'oldData' ,$oldData); $ this ->display(); } |
2.新建natureLists方法,新建View/Type/natureLists.html模板,载入类型属性列表页.
1 2 3 4 5 6 | public function natureLists(){ $tid = I( 'get.tid' ); $oldData = M( 'type_nature' )->where( 'shop_type_tid' ,$tid)->select(); p($oldData); $ this ->display(); } |
3 新建natureAdd方法,实现为该类型添加属性与编辑属性的功能.
3.1 在natureAdd方法中载入添加页面,并新建TypeNatureModel类,声明主键ID与表名,并加上自动验证
1 2 3 4 5 6 7 8 9 | class TypeNatureModel extends BaseModel{ protected $pk = 'nid' ; protected $tableName = 'type_nature' ; protected $_validate = array( array( 'nname' , 'require' , '属性名称不能为空!' ), array( 'nvalue' , 'require' , '属性值不能为空!' ), array( 'nname' , '' , '该属性名称已经存在!' ,0, 'unique' ,1), ); } |
3.2 判断IS_POST,并用变量接收post数组,追加type_tid,并调用store方法,并新增一个回调函数,以跳转至属性列表页
1 2 3 4 5 6 7 8 9 10 11 12 | public function natureAdd(){ if (IS_POST){ $data = I( 'post.' ); $tid = I( 'get.tid' ); $data[ 'type_tid' ] = $tid; $ this ->store( new TypeNatureModel(),$data, function ($result){ $tid = $result[ 'tid' ]; $ this ->success( '成功添加属性' , "/Shop/index.php/Admin/Type/natureLists/tid/$tid" ); }); } $ this ->display(); } |
3.3 在BaseModel类中增加一个变量接收$data中的tid,并return给回调函数,然后在回调函数中的success地址栏增加tid
1 2 3 4 5 6 7 8 9 10 11 12 | class BaseModel extends Model{ public function store($data){ if ($ this ->create($data)){ $action = isset($data[$ this ->pk]) ? 'save' : 'add' ; $ this ->$action($data); $tid = $data[ 'type_tid' ]; return [ 'status' => 'success' , 'message' => '操作成功' , 'tid' => "$tid" ]; } else { return [ 'status' => 'failed' , 'message' =>$ this ->getError()]; } } } |
3.4 修改编辑按钮地址栏参数,新增一个参数nid{:U('Admin/Type/natureAdd',['tid'=>I('get.tid'),'nid'=>$v['nid']])}
在TypeController中声明一个$status属性,在natureAdd方法中改变该属性的值为 "添加",用变量接收GET中的nid
用来区别是添加模式还是编辑模式,分别判断2个nid是否存在,存在则为编辑,分配旧数据,不存在则为添加.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public function natureAdd(){ $ this ->status = '添加' ; $nid = I( 'get.nid' ); if ($nid){ $oldData = M( 'type_nature' )->where( "nid=$nid" )->find(); $ this ->assign( 'oldData' ,$oldData); $ this ->status = '修改' ; } $ this ->assign( 'status' ,$ this ->status); if (IS_POST){ $data = I( 'post.' ); $tid = I( 'get.tid' ); $data[ 'type_tid' ] = $tid; if ($nid){ $data[ 'nid' ] = $nid; } $ this ->store( new TypeNatureModel(),$data, function ($result){ $tid = $result[ 'tid' ]; $ this ->success( '成功' . $ this ->status . '属性' , "/Shop/index.php/Admin/Type/natureLists/tid/$tid" );die; }); } $ this ->display(); } |
4.新建natureDel方法,实现删除属性的功能
用变量接收Get中的nid,进行删除
1 2 3 4 5 6 7 8 9 | public function natureDel(){ $nid = I( 'get.nid' ); $result = M( 'type_nature' )-> delete ($nid); if ($result == false ){ $ this ->error( '删除失败,SQL出错' ); } else { $ this ->success( '删除成功' ); } } |
三 add方法中加入变量判断,区别添加和编辑,实现编辑功能
1 新建构造方法,调用status属性,声明值为"添加"
2 在add方法中用变量接收地址栏tid,判断tid是否存在以区别编辑还是添加功能.
3 分配旧数据,在模型类补加自动验证,增加闭包函数,实现添加和修改完跳转至列表页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public function add(){ $tid = I( 'get.tid' ); if ($tid){ $ this ->status = '修改' ; $oldData = M( 'type' )->find($tid); $ this ->assign( 'oldData' ,$oldData); } $ this ->assign( 'status' ,$ this ->status); if (IS_POST){ $data = I( 'post.' ); if ($tid){ $data[ 'tid' ] = $tid; } $ this ->store( new TypeModel(),$data, function (){ $ this ->success( '成功' . $ this ->status . '类型' , '/Shop/index.php/Admin/Type/lists' );die; }); } $ this ->display(); } |
四 新建del方法,实现对类型的删除功能
用变量接收GET中的tid,进行删除
1 2 3 4 5 6 7 8 9 | public function del(){ $tid = I( 'get.tid' ); $result = M( 'type' )-> delete ($tid); if ($result == false ){ $ this ->error( '删除失败,SQL出错' ); } else { $ this ->success( '删除成功' ); } } |
五 新建CategoryController分类管理类,实现对分类的增删改查功能
1 新建Admin/Controller/Category控制器,Common/Model/Category模型类,声明主键ID与自动验证
1 2 3 4 5 6 7 8 9 | class CategoryModel extends BaseModel{ protected $pk = 'cid' ; protected $tableName = 'category' ; protected $_validate = array( array( 'cname' , 'require' , '类型名称不能为空!' ), array( 'cname' , '' , '该类型名称已经存在!' ,0, 'unique' ,1), array( 'csort' , 'require' , '排序不能为空!' ), ); } |
2 在控制器中新建lists方法,add方法,分别载入列表页和添加页面.在lists方法分配旧数据,add方法添加数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class CategoryController extends AdminController{ public $status; public function __construct(){ parent::__construct(); $ this ->status = '添加' ; } public function lists(){ $oldData = M( 'category' )->select(); $ this ->assign( 'oldData' ,$oldData); $ this ->display(); } public function add(){ if (IS_POST){ $data = I( 'post.' ); $ this ->store( new CategoryModel(),$data, function (){ $ this ->success( '成功' . $ this ->status . '分类' ); }); } $ this ->display(); } } |
3 在add方法中用变量接收,通过2次判断,区分编辑模式和添加模式
3.1 在ControllerModel中分别新建getCateData,getSon方法用来获取编辑的分类下拉框中的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public function add(){ if ($ this ->cid){ $ this ->status = '编辑' ; $oldData = M( 'category' )->find($ this ->cid); // p($oldData); $ this ->assign( 'oldData' ,$oldData); $cateData = ( new CategoryModel())->getCateData($ this ->cid); // p($cateData); $ this ->assign( 'cateData' ,$cateData); } $ this ->assign( 'status' ,$ this ->status); if (IS_POST){ $data = I( 'post.' ); if ($ this ->cid){ $data[ 'cid' ] = $ this ->cid; } $ this ->store( new CategoryModel(),$data, function (){ $ this ->success( '成功' . $ this ->status . '分类' , '/Shop/index.php/Admin/Category/lists' ); }); } $ this ->display(); } |
3.2 在getCateData方法中调用getSon方法,递归找寻自己的子分类的ID
3.3 复制HDPHP中的vendor中的Data/data.php至ThinkPHP/Library下(类库扩展),并声明命名空间,直接实例化,可调用tree树形结构方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public function getCateData($cid){ $data = M( 'category' )->select(); // p($data); $cids = $ this ->getSon($data,$cid); $cids[] = $cid; $cids = implode( ',' ,$cids); // p($cids); $cateData = ( new CategoryModel())->query( "SELECT * FROM shop_category WHERE cid NOT IN ($cids)" ); // p($cateData); $cateData = ( new Data())->tree($cateData, 'cname' ); return $cateData; } public function getSon($data,$cid){ static $temp = []; foreach($data as $v){ if ($cid == $v[ 'pid' ]){ $temp[] = $v[ 'cid' ]; $ this ->getSon($data,$v[ 'cid' ]); } } return $temp; } |
4 新建del方法,先获取当先类的pid,然后替换掉自己子分类的pid,删除当前分类
1 2 3 4 5 6 7 8 9 10 11 | public function del(){ $pid = M( 'category' )->find($ this ->cid); $pid = $pid[ 'pid' ]; M( 'category' )->where( "pid=$this->cid" )->setField( 'pid' ,$pid); $result = M( 'category' )-> delete ($ this ->cid); if ($result){ $ this ->success( '删除成功' ); } else { $ this ->error( '删除失败' ); } } |
