刪除(真刪除、軟刪除)很簡單
在User.php控制器添加刪除操作
<?php namespace app\frontend\controller; use \tpfcore\Core; class User extends FrontendBase { public function add() { IS_POST && $this->jump(Core::loadModel($this->name)->saveUser($this->param)); return $this->fetch("add"); } public function edit(){ IS_POST && $this->jump(Core::loadModel($this->name)->editUser($this->param)); return $this->fetch("edit",[ "list"=>Core::loadModel($this->name)->listUser($this->param) ]); } public function del(){ $this->jump(Core::loadModel($this->name)->delUser($this->param)); } }
在service層添加User.php
<?php // +---------------------------------------------------------------------- // | Author: yaoyihong <510974211@qq.com> // +---------------------------------------------------------------------- namespace app\frontend\service;
use app\common\service\ServiceBase;
use \tpfcore\Core;
/**
* 基礎(chǔ)服務(wù)
*/
class User extends FrontendBase
{
public function delUser($data){
$validate=\think\Loader::validate($this->name);
$validate_result = $validate->scene('del')->check($data);
if (!$validate_result) {
return [RESULT_ERROR, $validate->getError(),null];
}
return Core::loadModel($this->name)->delUser($data);
}
}
public function delUser($data){ $result=self::deleteObject($data,true); if($result){ return [RESULT_SUCCESS, "操作成功",null]; }else{ [RESULT_ERROR, "操作失敗",null]; } }
del驗證規(guī)則自己去寫...
這就完成一個簡單的刪除功能了
刪除分真刪除與軟刪除(改變狀態(tài)),默認(rèn)是軟刪除,狀態(tài)字段對應(yīng)的是status (0正常,-1刪除)
真刪除直接調(diào)用deleteObject(where,true)即可,where為你要刪除的條件
軟刪除的話第二個參數(shù)為false,但是,如果你的刪除標(biāo)識字段不是status,你就得這樣調(diào)用deleteObject(where,false,"刪除字段標(biāo)識")
更多建議: