建设论坛网站大全,高校网站群建设方案,京东网站建设现状,软装设计公司名称查询多条数据#xff1a;all( )方法all方法与前节课学习的get方法都是静态方法#xff0c;可用模型类直接访问2. 源码#xff1a;/*** 查找所有记录* access public* param mixed $data 主键列表或者查询条件(闭包)* param array|string $with 关联预查询* param b…查询多条数据all( )方法all方法与前节课学习的get方法都是静态方法可用模型类直接访问2. 源码/*** 查找所有记录* access public* param mixed $data 主键列表或者查询条件(闭包)* param array|string $with 关联预查询* param bool $cache 是否缓存* return static[]|false* throws exception\DbException*/public static function all($data null, $with [], $cache false){ $query static::parseQuery($data, $with, $cache); return $query-select($data);}all方法源码与get源码几乎一样仅是最后调用的查询方法不同select($data)3. 参数与返回值参数序号参数说明1数字/字符串主键列表2查询表达式支持所有查询表达式3闭包函数支持更多高级查询语法返回值数据集对象。4. 实例演示我们仍以tp5_staff数据表为例一、任务1获取tp5_staff表中id等于1009和1010的记录控制器:Index.php查询条件以字符串方式给出’10091010‘?phpnamespace app\index\controller;//导入模型类use app\index\model\Staff;class Index { public function index(){//1.执行查询,返回数据对象数组 $result Staff::all(1009,1010);//2.遍历该数据对象数组:$result //$data既是循环变量,也是其中一个数据对象 foreach ($result as $data){ //getData()可以获取数据对象原始数据:$data属性值 dump($data - getData());}}}all( ) 方法的主键列表还可以用数组表示//1.执行查询,返回数据对象数组 $result Staff::all([1009,1010]);以上查询方法返回的SQL查询语句都是一样的SELECT * FROM tp5_staff WHERE id IN (1009,1010) ;二、任务2查询表中年龄age大于30并且工资salary大于800的员工信息显然对于这样需求上面方法无能为力只能通过构造查询表达式来解决控制器Index.php?phpnamespace app\index\controller;//导入模型类use app\index\model\Staff;class Index { public function index(){//1.构造查询表达式 $map[age] [,30]; $map[salary] [,8000]; //2.执行查询,返回数据对象数组 $result Staff::all($map);//3.遍历该数据对象数组:$result //$data既是循环变量,也是其中一个数据对象 foreach ($result as $data){ //getData()可以获取数据对象原始数据:$data属性值 dump($data - getData());}}}对应的SQL语句SELECT * FROM tp5_staff WHERE age 30 AND salary 8000;查询结果如下三、任务3在任务2的基础上(age30 AND salary8000)我们又提出了三个需求按工资排序只输出工资最高的3个人的编号,姓名,年龄,工资信息。根据需求查询表达式已无法完成必须借助连贯方法就这要用到闭包查询控制器Index.php?phpnamespace app\index\controller;//导入模型类use app\index\model\Staff;class Index { public function index(){//1.构造闭包函数 $closure function ($query){ //1.设置字段别名 $field[id] 编号; $field[name] 姓名; $field[age] 年龄; $field[salary] 工资; //2.设置查询表达式 $map[age] [,30]; $map[salary] [,8000];//3.执行查询 $query - field($field) //限制显示字段 - where($map) //过滤查询结果 - order(salary desc) //按salary字段降序输出 - limit(3); //限制输出数量 }; //2.执行闭包查询,返回数据对象数组 $result Staff::all($closure);//3.遍历该数据对象数组:$result //$data既是循环变量,也是其中一个数据对象 foreach ($result as $data){ //getData()可以获取数据对象原始数据:$data属性值 dump($data - getData());}}}查询对应的SQL语句SELECT id AS 编号,name AS 姓名,age AS 年龄,salary AS 工资 FROM tp5_staff WHERE age 30 AND salary 8000 ORDER BY salary desc LIMIT 3浏览器查看array(4) {[编号] int(1006)[姓名] string(9) 西门庆[年龄] int(90)[工资] float(20301)}array(4) {[编号] int(1002)[姓名] string(6) 帮主[年龄] int(255)[工资] float(12345)}array(4) {[编号] int(1028)[姓名] string(6) 方方[年龄] int(90)[工资] float(10877)}数据库查询结果SQLPRO for MySQL工具5. 总结all( )方法与查询类的select方法的功能是一样的你完成可以认为这是省去了选择数据表的select操作。其实我们将闭包查询中的all( ),换成:select( )方法查询结果是一样的$result Staff::select($closure);与$result Staff::all($closure);完全是等价的为什么会是这样的呢Model类中并没有select静态方法呀这就是__callStatic( )魔术方法的魔力发挥了作用记不清的同学请复习OOP编程总结章节。