栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

thinkphp6模型一般使用方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

thinkphp6模型一般使用方法

public function deleteTest(){
    //批量软删除
   $res=$this->destroy(function ($query){
       $query->where([
           ['user_name','=','占山2']
       ]);
   });
    //单个软删除1
    $res=$this->destroy(['id'=>14]);
    //单个软删除2
    $res=$this->destroy(['user_name'=>'占山2']);
    //单个软删除3
    $res=$this->destroy(14);
   print_r($res);
   die();
}

public function addTest(){
    //单个新增加(支持时间自动写入)
    $data = [
        'user_name'=>'你猜'
    ];
    $this->save($data);
   //获取自增id
    echo $this->id;
    //saveAll方法如果数据中包含主键,那么就是更新,不包含主键,就是新增加(支持时间自动写入)
    $datas = [
        ['user_name'=>'你猜','header_img'=>123],
       ['user_name'=>'你猜2','header_img'=>123],
        ['user_name'=>'你猜3','header_img'=>123],
    ];
   $this->saveAll($datas);
}


public function updateTest(){
   $update['id'] = 1;
   $input['role_id'] = 15;
    $update['last_login_time'] = time();
    if (true){
        $update['status'] = 3;
    }
    $res = $this->update($update,['id'=>1]);
    print_r($res);
    die();
    //支持自动时间写入更新
    UserRole::update([
        'role_id'=>$input['role_id'],
   ],['user_id'=>1]);
    //不支持自动更新时间
    $this->where([
        'id'=>14
   ])->update([
       'user_name'=>'你好哈哈',
   ]);

    //单个数据更新
    $this->update([
       'user_name'=>'你好哈哈',
        'id'=>14
    ]);

    //批量数据更新(如果此id没有也不会插入数据)
    $date = [
       ['id'=>12,'user_name'=>'你猜'],
        ['id'=>23,'user_name'=>'你猜2']
   ];
   $this->saveAll($date);
   die();
    //特殊更新方法(支持自动时间写入)
   $this->update([
       'id'=>13,
      'login_error_num'=>Db::raw('login_error_num+1')
   ]);

}


public function selectData(){
    //查询单个数据1
    $res=$this->select([12,13]);
   if ($res){
       $res = $res->toArray();
    }
    //查询单个数据2
    $res = $this->where('user_name','你猜')->find();

    //查询单个数据3(该方法为错误示范)find方法只能填id值
   $res = $this->find([
      'user_name'=>'你猜'
   ]);
  halt($res);
    //简单的关联查询
    $res=$this->find(1);
    if ($res){
        //user_role 关联方法名
        $res->user_role;
   }
    $res = $res->toArray();
    echo "
";
    print_r($res);
    die();
   //多个数据查询(如果没找到数据返回一个空数组)
  $res =$this->where('user_name','张山')->select()->toArray();

    //多个数据查询(如果没找到数据返回一个空数组)
    $res =$this->where([
        ['id','<>',1]
    ])->field('')->select()->toArray();
    $where[] =['id','<>',1];
    $field = 'id';
    $res =$this->getDateByIds($where,$field);
    halt($res);

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号