thinkCMF的增删改查基本操作:

一、增加数据

$res = Db::name('form')->insert($data);

示例代码:

public function index(){
    if(!request()->isPost()){exit('请求错误');};
    $return = array();
    $return['code'] = 1;
    $return['msg'] = "添加成功";
    $data = array();
    $data['username'] = input('username');
    $data['company'] = input('company');
    $data['phone'] = input('phone');
    $data['email'] = input('email');
    $data['time'] = time();
    $data['message'] = input('message');
    $res = Db::name('form')->insert($data);
    if(!$res){
        $return['code'] = 0;
        $return['msg'] = "添加失败";
    };
    exit(json_encode($return));
}

二、删除数据

Db::name('form')->delete($id);

简单示例:

public function del(){
    $id = $this->request->param('id', 0, 'intval');
    Db::name('form')->delete($id);
    $this->success("删除成功!", url("form/index"));
}

三、修改数据

$res = Db::name('model')->update($data);

简单示例:

public function editPost(){
    if(!request()->isPost()){exit('请求错误');};
    $data = $this->request->param();
    $return = array();
    $return['code'] = 1;
    $return['msg'] = '修改成功';
    $res = Db::name('model')->update($data);
    if(!$res){
        $return['code'] = 0;
        $return['msg'] = "修改失败";
    };
    exit(json_encode($return));
}

四、查询数据

$category_list = Db::name('portal_category')->field(array('id','parent_id'))->select();
$ids = Db::name('portal_category_post')->where('category_id','in',$cur_category_ids)->field(array('post_id'))->select();
$categoryinfo = Db::name('portal_category_post')->where('post_id','eq',$id)->field('category_id')->find(); 
$slide = Db::name('slide_item')->where($where)->select();

结合where使用:

$where = array();
$where['id'] = array('in',$all_post_ids_arr);
$where['post_type'] = 1;
$where['post_status'] = 1;
$pagelist_arr = Db::name('portal_post')->where($where)->paginate($limit);