//封装前
public function del($id){
if(empty($id)){
return $this->error('非法操作!');
}
$map['id'] = array('IN' , $id);
$result = db('table')->where($map)->delete();
if( $result ) {
return $this->success('删除成功');
}
else {
return $this->error('删除失败');
}
}
//封装后
BaseModel
class Base extends \think\Model
{
protected $param;
protected $type = array(
'id' => 'integer' ,
'cover_id' => 'integer' ,
);
public function initialize()
{
parent::initialize();
$this->param = \think\Request::instance()->param();
}
/**
* 数据修改
* @return [bool] [是否成功]
*/
public function change()
{
$data = \think\Request::instance()->post();
if( isset($data['id']) && $data['id'] ) {
return $this->save($data , array('id' => $data['id']));
}
else {
return $this->save($data);
}
}
}
网友评论