all() select()查询多条数据的区别---ThinkPHP5(TP5)对数据库操作

select 与 all 均返回查询数据结果集合

$tag=Tag::select(4);

$tag=Tag::select([1,2,3]);

$tag=Tag::select('1,2,3');

$tag=Tag::all(4);

$tag=Tag::all([1,2,3]);

$tag=Tag::all('1,2,3');

$tag=Tag::where('is_show','in','1,0')->all();

$tag=Tag::where('is_show','in','1,0')->select();

all ()与select(),用法差距不大,all()底层最终还是调用的select()方法

/**

* 查找所有记录

* @access public

* @param  mixed        $data  主键列表或者查询条件(闭包)

* @param  array|string $with  关联预查询

* @param  bool        $cache 是否缓存

* @return static[]|false

* @throws exception\DbException

*/

public function all($data = null, $with = [], $cache = false)

{

    if (true === $with || is_int($with)) {

        $cache = $with;

        $with  = [];

}

    return $this->parseQuery($data, $with, $cache)->select($data);

}

/**

* 查找记录

* @access public

* @param  array|string|Query|\Closure $data

* @return Collection|array|\PDOStatement|string

* @throws DbException

* @throws ModelNotFoundException

* @throws DataNotFoundException

*/

public function select($data = null)

{

    if ($data instanceof Query) {

        return $data->select();

    } elseif ($data instanceof \Closure) {

        $data($this);

        $data = null;

}

    $this->parseOptions();

    if (false === $data) {

        // 用于子查询 不查询只返回SQL

        $this->options['fetch_sql'] = true;

    } elseif (!is_null($data)) {

        // 主键条件分析

        $this->parsePkWhere($data);

}

    $this->options['data'] = $data;

    $resultSet = $this->connection->select($this);

    if ($this->options['fetch_sql']) {

        return $resultSet;

}

    // 返回结果处理

    if (!empty($this->options['fail']) && count($resultSet) == 0) {

        $this->throwNotFound($this->options);

}

    // 数据列表读取后的处理

    if (!empty($this->model)) {

        // 生成模型对象

        $resultSet = $this->resultSetToModelCollection($resultSet);

    } else {

        $this->resultSet($resultSet);

}

    return $resultSet;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,048评论 0 2
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,430评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,504评论 0 13
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,910评论 0 0
  • Overview The ccxt library is a collection of available cr...
    郭蝈儿蝈儿阅读 3,799评论 0 1