美文网首页
all() select()查询多条数据的区别---ThinkP

all() select()查询多条数据的区别---ThinkP

作者: 思议岁月 | 来源:发表于2018-12-13 22:08 被阅读0次

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;

}

相关文章

网友评论

      本文标题:all() select()查询多条数据的区别---ThinkP

      本文链接:https://www.haomeiwen.com/subject/jerihqtx.html