Db('user')->where('id',1)->find()返回的是数组如果是非助手函数:
use app\admin\model\User as UserModel ;protected $user_model;
// 构造函数
function __construct()
{
parent::__construct();
$this->user_model = new UserModel();
}$this->user_model = new UserModel();
$this->user_model->where('id',1)->find();此时返回的是一个模型对象,查了下源码是因为 thinkphp\library\think\db\Query.php中,约2086行开始// 数据处理
if (!empty($result[0])) {
$data = $result[0];
if (!empty($this->model)) {
// 返回模型对象
$model = $this->model;
$data = new $model($data);
$data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null);
// 关联查询
if (!empty($options['relation'])) {
$data->relationQuery($options['relation']);
}
if (!empty($options['with'])) {
// 预载入
$data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : '');
}
}
}不明白这里为何要判断返回一个模型对象,我只是new model,按说应该返回的是正常的数组呀最佳答案