## 查询多条数据:all( )方法 >[info] 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); } ~~~ >[info] all方法源码与get源码几乎一样,仅是最后调用的查询方法不同:select($data) * * * * * #### 3. 参数与返回值: * 参数: | 序号 | 参数 | 说明 | | --- | --- | --- | | 1 | 数字/字符串 | 主键列表 | | 2| 查询表达式 | 支持所有查询表达式 | | 3 | 闭包函数 | 支持更多高级查询语法 | * 返回值:数据集对象。 * * * * * #### 4. 实例演示 >[info] 我们仍以tp5_staff数据表为例 一、任务1:获取tp5_staff表中:id等于1009和1010的记录 * 控制器:Index.php > 查询条件以字符串方式给出:’1009,1010‘ ~~~ <?php namespace 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的员工信息 >[info] 显然,对于这样需求,上面方法无能为力,只能通过构造查询表达式来解决 * 控制器:Index.php ~~~ <?php namespace 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; ~~~ * 查询结果如下: ![](https://box.kancloud.cn/80c6bb85921c7ae14409e5972b980d60_715x355.png) * * * * * 三、任务3:在任务2的基础上(age>30 AND salary>8000),我们又提出了三个需求:按工资排序,只输出工资最高的3个人的编号,姓名,年龄,工资信息。 >[info] 根据需求,查询表达式已无法完成,必须借助连贯方法,就这要用到:闭包查询 * 控制器:Index.php ~~~ <?php namespace 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工具 ![](https://box.kancloud.cn/7ee2c1ef33d0f1faa90e18012bcf04ca_772x359.png) * * * * * #### 5. 总结: >[success] all( )方法与查询类的select方法的功能是一样的,你完成可以认为这是省去了选择数据表的select操作。 * 其实我们将闭包查询中的all( ),换成:select( )方法,查询结果是一样的 ~~~ $result = Staff::select($closure); ~~~ 与 ~~~ $result = Staff::all($closure); ~~~ 完全是等价的! >[warning] 为什么会是这样的呢?Model类中并没有select静态方法呀!这就是__callStatic( )魔术方法的魔力发挥了作用!记不清的同学,请复习OOP编程总结章节。