thinkphp5常用函数汇总_find() · 常用的thinkPHP5内置函数和PHP7原生函数 · 看云

"本文详细解析了thinkDb类中的find()方法,该方法用于查找单条记录,返回一维数组。当配合使用时,二维数组的取值方式有所不同。find()在找不到数据时会触发异常,内容涵盖了查询表达式分析、主键条件处理、缓存机制、数据处理等核心步骤。此外,还介绍了如何处理查询结果以及关联查询的相关操作。"
摘要由CSDN通过智能技术生成

find()返回一个一维数组,select()返回一个二维数组,所以在取值时有所不同,一维数组取值用$data["n"],二维数组取值用$data[0]["n"]

* * * * *

find()

think\Db

class Query

~~~

/**

* 查找单条记录

* @access public

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

* @return array|false|\PDOStatement|string|Model

* @throws DbException

* @throws ModelNotFoundException

* @throws DataNotFoundException

*/

public function find($data = null)

{

if ($data instanceof Query) {

return $data->find();

} elseif ($data instanceof \Closure) {

call_user_func_array($data, [ & $this]);

$data = null;

}

// 分析查询表达式

$options = $this->parseExpress();

$pk = $this->getPk($options);

if (!is_null($data)) {

// AR模式分析主键条件

$this->parsePkWhere($data, $options);

} elseif (!empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['AND'][$pk])) {

$key = $this->getCacheKey($options['where']['AND'][$pk], $options, $this->bind);

}

$options['limit'] = 1;

$result = false;

if (empty($options['fetch_sql']) && !empty($options['cache'])) {

// 判断查询缓存

$cache = $options['cache'];

if (true === $cache['key'] && !is_null($data) && !is_array($data)) {

$key = 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;

} elseif (is_string($cache['key'])) {

$key = $cache['key'];

} elseif (!isset($key)) {

$key = md5(serialize($options) . serialize($this->bind));

}

$result = Cache::get($key);

}

if (false === $result) {

// 生成查询SQL

$sql = $this->builder->select($options);

// 获取参数绑定

$bind = $this->getBind();

if ($options['fetch_sql']) {

// 获取实际执行的SQL语句

return $this->connection->getRealSql($sql, $bind);

}

if (is_string($pk)) {

if (!is_array($data)) {

if (isset($key) && strpos($key, '|')) {

list($a, $val) = explode('|', $key);

$item[$pk] = $val;

} else {

$item[$pk] = $data;

}

$data = $item;

}

}

$options['data'] = $data;

// 事件回调

if ($result = $this->trigger('before_find', $options)) {

} else {

// 执行查询

$resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_pdo']);

if ($resultSet instanceof \PDOStatement) {

// 返回PDOStatement对象

return $resultSet;

}

$result = isset($resultSet[0]) ? $resultSet[0] : null;

}

if (isset($cache) && false !== $result) {

// 缓存数据

$this->cacheData($key, $result, $cache);

}

}

// 数据处理

if (!empty($result)) {

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

// 返回模型对象

$model = $this->model;

$result = new $model($result);

$result->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null);

// 关联查询

if (!empty($options['relation'])) {

$result->relationQuery($options['relation']);

}

// 预载入查询

if (!empty($options['with'])) {

$result->eagerlyResult($result, $options['with']);

}

// 关联统计

if (!empty($options['with_count'])) {

$result->relationCount($result, $options['with_count']);

}

}

} elseif (!empty($options['fail'])) {

$this->throwNotFound($options);

}

return $result;

}

~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值