• thinkphp5的with函数


    使用场景

    with使用在关联模型的查询中,支持链式操作

    with本质

    本质上是一个函数,支持一些参数

    有人说是这样的
    with('模型中定义的方法名')方法
    

    官方对with的解释,他妈的,真恶心

    看了半天,不知道with函数的参数是什么

    解决办法

    只有查看with函数的源码

    如何查找with函数的原始定义位置

    按道理会在think的model基类中,然而在基类model中,并没有找到with函数的定义。
    我们继续。

    最终发现 with()函数的定义在
    /thinkphp/library/think/db/Query.php中
    

    关于with的函数有三个,这时候,我们要分析函数了

    分析函数的方法是,分析函数的三要素(函数名,参数,返回值),以及函数的功能。
    那我们贴上和with相关的三个函数

    ///thinkphp/library/think/db/Query.php文件中的,2061行到2169行如下
    /**
         * 设置关联查询JOIN预查询
         * @access public
         * @param string|array $with 关联方法名称
         * @return $this
         */
        public function with($with)
        {
            if (empty($with)) {
                return $this;
            }
    
            if (is_string($with)) {
                $with = explode(',', $with);
            }
    
            $first = true;
    
            /** @var Model $class */
            $class = $this->model;
            foreach ($with as $key => $relation) {
                $subRelation = '';
                $closure     = false;
                if ($relation instanceof Closure) {
                    // 支持闭包查询过滤关联条件
                    $closure    = $relation;
                    $relation   = $key;
                    $with[$key] = $key;
                } elseif (is_array($relation)) {
                    $subRelation = $relation;
                    $relation    = $key;
                } elseif (is_string($relation) && strpos($relation, '.')) {
                    $with[$key]                   = $relation;
                    list($relation, $subRelation) = explode('.', $relation, 2);
                }
    
                /** @var Relation $model */
                $relation = Loader::parseName($relation, 1, false);
                $model    = $class->$relation();
                if ($model instanceof OneToOne && 0 == $model->getEagerlyType()) {
                    $model->eagerly($this, $relation, $subRelation, $closure, $first);
                    $first = false;
                } elseif ($closure) {
                    $with[$key] = $closure;
                }
            }
            $this->via();
            if (isset($this->options['with'])) {
                $this->options['with'] = array_merge($this->options['with'], $with);
            } else {
                $this->options['with'] = $with;
            }
            return $this;
        }
    
        /**
         * 关联统计
         * @access public
         * @param string|array $relation 关联方法名
         * @param bool         $subQuery 是否使用子查询
         * @return $this
         */
        public function withCount($relation, $subQuery = true)
        {
            if (!$subQuery) {
                $this->options['with_count'] = $relation;
            } else {
                $relations = is_string($relation) ? explode(',', $relation) : $relation;
                if (!isset($this->options['field'])) {
                    $this->field('*');
                }
                foreach ($relations as $key => $relation) {
                    $closure = $name = null;
                    if ($relation instanceof Closure) {
                        $closure  = $relation;
                        $relation = $key;
                    } elseif (!is_int($key)) {
                        $name     = $relation;
                        $relation = $key;
                    }
                    $relation = Loader::parseName($relation, 1, false);
    
                    $count = '(' . $this->model->$relation()->getRelationCountQuery($closure, $name) . ')';
    
                    if (empty($name)) {
                        $name = Loader::parseName($relation) . '_count';
                    }
    
                    $this->field([$count => $name]);
                }
            }
            return $this;
        }
    
        /**
         * 关联预加载中 获取关联指定字段值
         * example:
         * Model::with(['relation' => function($query){
         *     $query->withField("id,name");
         * }])
         *
         * @param string | array $field 指定获取的字段
         * @return $this
         */
        public function withField($field)
        {
            $this->options['with_field'] = $field;
            return $this;
        }
    
    

    但是,源码,我也是没看懂!擦!

    继续,看看别人网友的使用方法

    问题:多对多模型,能使用with函数吗

    参考文章
    TP5.1多对多关联添加查询条件
    https://blog.csdn.net/weixin_39429350/article/details/98599169
    看来,with函数,不支持多对多模型的查询

  • 相关阅读:
    【Python编程:从入门到实践】chapter6 字典
    【Python编程:从入门到实践】chapter5 if语句
    【Linux_Unix系统编程】Chapter9 进程凭证
    【Linux_Unix系统编程】Chapter10 时间
    【Linux_Unix系统编程】Chapter8 用户和组
    【Linux_Unix系统编程】chapter7 内存分配
    【Linux_Unix系统编程】chapter6 进程
    书籍 人生
    流程图软件
    技术文章,iOS,iOS开发系列 数学函数
  • 原文地址:https://www.cnblogs.com/cn-oldboy/p/13026257.html
Copyright © 2020-2023  润新知