thinkPHP数据模型杂记(自己总结 仅供自己使用)

public function test() {
//Debug::dump (Db::table ('message')->where ('uid=10002')->value ('title','你是谁啊!'));

//column 方法,将查询结果以一种很灵活的方式返回,让程序员有了更大的发挥空间处理数据,一定要熟练掌握它 一个参数为一维数组 两参数为二维数组 第二个参数将为键名 可以进行数组的重组

//dump( Db::table('tp5_staff') ->field( '*' ) -> select(1010) );

/*
 * 1、插入记录,并返回受影响记录数,通常为1
 * Db::table( 完整数据表 )  -> insert( $data );
 * 2、插入记录,返回新增主键
 * Db::table( 完整数据表 )  -> insert( $data,false,true );
 * 3、插入记录,返回新增主键的另一种实现形式
 * 返回新增主键ID使用频繁,因此有一个专门方法:insertGetId($data),专门用于插入数据并返回自增主键
 * Db::table( 完整数据表 )   ->  insertGetId($data);
 *
 *
 * 更新:
 * 单条更新:
 * Db::table('tp5_staff') -> where('id',1024) -> setField('salary' , 8500);
 *
 *  update setField 更新必须传入数组字段id
 *
 * setField 批量更新必须给出更新条件
 *
 * 1.update方法与setField方法都可以完成同样的工作;
 * 2.日常开发中,推荐使用update方法;
 * 3.当仅更新单条记录中某个字段值时,用setField方法更简洁和直观。
 *
 * 自增和自减在完成大量的数组自增自减时推荐使用
 *
 *
 * 条件写在参数中
 * Db::table( 表名 ) -> delete( 条件表达式 );
 *
 * 清空数据表
 * Db::table( 表名 ) -> delete( true );
 *
 * 前置条件where方法
 * Db::table( 表名 ) -> where( 条件表达式 ) -> delete(  );
 *
 * 获取表信息
 * Db::getTableInfo('message')
 *
 * 掌握闭包查询
 *
 * 将多个条件进行打包 进行产查询
 *
 * public function index(){
 * //查询 dept(部门)等于 1 的员工信息
 *  $result = Db::table('tp5_staff')   // 设置数据表
 * -> field('id,name,dept')// 设置结果集中允许显示的字段
 * -> where(function($query){
 * $query -> where('dept = 1')
 * -> whereOr('sex = 1') ;
 * })
 *  -> where('id between 1010 and 1020')  //设置id > 1010 and id < 1020之间
 * -> select();   // 获取结果集
 * //查看结果
 * dump($result);
 * }
 *
 *
 * 集合用字符串表示
 * $result = Db::table('tp5_staff') -> where('id','in','1003,1005,1020') -> select();
 *
 * // 取反,即不在某个集合内  not in
 * $result = Db::table('tp5_staff') -> where('id','not in','1003,1005,1020') -> select();
 * //集合用数组表示
 * $result = Db::table('tp5_staff') -> where('id','in',[1003,1005,1020]) -> select();
 * // 取反,即不在某个集合内  not in
 * $result = Db::table('tp5_staff') -> where('id','not in',[1003,1005,1020]) -> select();
 *
 *
 * //判断sex字段是否存在值 null
 * $result = Db::table('tp5_staff') -> where('sex','null')-> select();
 *
 * //条件取反:不为空 not null
 * $result = Db::table('tp5_staff') -> where('sex','not null')-> select();
 *
 * //如果要判断一个字段值是否为空,要三个参数,中间为'='
 * $result = Db::table('tp5_staff') -> where('sex','=','null')-> select();
 *
 * //条件取反:不为空 not null
 * $result = Db::table('tp5_staff') -> where('sex','=','not null')-> select();
 *
 * 
 *
 * */

Debug::dump (Db::getTableInfo('message'));
}




public function index(){
   
   //查询部门的平均工资,小数保留2位
   $result = Db::table('tp5_staff')
   		//字段名称中,可以使用聚合统计函数
  -> field(['dept'=>'部门','ROUND(AVG(salary),2)'=>'平均工资'])
  -> group('dept')->select();   
 
//查看结果
dump($result);
  }


public function index(){
   
   //查询部门的平均工资,小数保留2位
   $result = Db::table('tp5_staff')
   		//字段名称中,可以使用聚合统计函数
  -> field(['dept'=>'部门','ROUND(AVG(salary),2)'=>'平均工资'])
  -> group('dept')
  -> having('avg(salary) > 6400')
  -> select();   
 
//查看结果
dump($result);
  }



public function index(){   
  
  //查询tp5_staff表中工资大于9500元的员工信息(姓名与工资)
   $result = Db::table('tp5_staff')// 选择数据表
  -> field(['name' => '姓名','salary' => '工资'])  //设置字段与字段别名
  -> where(['salary'=>['>',9500]])  //设置过滤条件
  -> order(['salary' => 'desc'])   // 设置结果集记录排序字段与排列顺序
  ->select();   // 输出结果集
 
//查看结果
dump($result);
  }


public function index(){   
   $result = Db::table('tp5_staff')// 设置数组表tp5_staff
  -> field(['name' => '姓名','salary' => '工资'])  // 仅显示姓名和工资,用别名
  -> order(['salary' => 'desc'])  // 按工资降序由高到底排列
  -> limit(3)   // 仅显示前三条记录,即工资最高的员工
  // -> limit( '3' )  //用字符串也可以,limit 会自动转换为数值型
  ->select();  // 返回结果集
 
//查看结果
dump($result);
  }


public function index(){   
   $result = Db::table('tp5_staff')// 设置数组表tp5_staff
  -> field(['name' => '姓名','salary' => '工资'])  // 仅显示姓名和工资,用别名
  -> order(['salary' => 'desc'])  // 按工资降序由高到底排列
  -> limit(4,3)   // 因为从0开始计数,所以4代表起始位置为第5条记录,
  // -> limit( '4, 3' )  // 用字符串也可以,limit 会自动转换为数值型
  ->select();  // 返回结果集
 
//查看结果
dump($result);
}

  
  public function index(){   

  //1.设置视图查询条件
  $table1 = ['tp5_staff'=>'a'];   //设置第1张表及表别名
  $table2 = ['tp5_dept'=>'b'];//设置第2张表及表别名
  $field1 = ['id'=>'编号','name' => '姓名','salary' => '工资']; //设置第1张表字段别名
  $field2 = ['dept_name' => '部门'];  //设置第2张表字段别名
  $on = 'a.dept = b.id';   //设置2张表连接条件
  $type = 'LEFT';//设置连接类型为左连接

  //2.设置查询条件(数组方式进行批量设置)  
  $where = [];
  $where['age'] = ['>=',40];
  $where['salary'] = ['<=',8000]; 

  //3.设置排序条件
  $order = ['salary'=>'desc'];

  //4.设置输出数量
  $num = 3;

  
  //1.执行多表视图查询
  $result = Db::view($table1,$field1)//设置第1张表的表名与字段名
  -> view($table2,$field2,$on,$type)  //设置第1张表的表名与字段名,连接条件和连接类型
  -> where($where)//设置查询条件
  -> order($order)//设置结果排序条件
  -> limit($num)   // 设置输出记录数量
  -> select();//获取结果集

  //4.输出结果
  dump($result);
  }
  
  
  public function index(){ 

//1.生成子查询闭包:查询tp5_good表中good=1的员工id
$subQuery = function($query){
   $query -> table('tp5_good')  //设置数据表,不允许同表查询
  -> field('id')   //字段必须与父查询的条件字段一致
  -> where('good','=',1); //设置子查询条件:good = 1 即优秀  
};
  
 //2.执行父查询:将子查询
$result = Db::table('tp5_staff')   //设置数据表
  -> field('id,name,salary')  //设置结果集字段列表
  -> where('id','in',$subQuery)  //将子查询闭包传给父查询字段IN条件
  -> select();  //获取结果集

//3.查看结果
dump($result);  
  }

  
  
  
  模型对象:
  
  
  
  功能:给模型对象$data赋值,将模型对象转为数据对象
  
  public function index(){ 

  	//1.创建模型对象$model
  	$model = new Staff();

  	//2.创建数组$data,用作数据对象数据源
  	$data = [];
  	$data['id'] = 1010;
  	$data['name'] = 'Peter';
  	$data['age'] = 50;  	

  	//3.调用模型对象data方法,将$data数组赋值该对象的$data属性
//完成数据对象的创建
  	$model -> data($data);

  	//3.查看对象
  	dump($model);	
  
  }
  
  
  
  setAttr方法
  
	如果数据对象中已存在指定数据,则更新;如果不存在,则创建。
	
	
	获取器 在模型中使用 可以转换其字段  
	
	
	修改器 将获得的数据的值 改为想要的值 进行插到数据库当中
	
	自动更新时间 
	
	修改配置文件  修改模型  手动修改数据
	
	路由
	/application/route.php 文件内容如下:
	<?php
	use thinkRoute;  //导入Route类
	Route::rule('demo/','index/index/demo/');  //创建路由规则
	可以将demo/ 理解为index/index/demo/的简写或等量替换
	
	带参数的路由:
	Route::rule('demo/[:study]/[:name]','index/Index/demo/');
原文地址:https://www.cnblogs.com/zhnaglei/p/8151120.html