模型和数据库查询有什么区别?

浏览:2237 发布日期:2017/01/12 分类:求助交流 关键字: find select query 疑问
如果用助手函数比如: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,按说应该返回的是正常的数组呀

最佳答案
评论( 相关
后面还有条评论,点击查看>>