Think/Model/ViewModel.php
protected function checkFields($fields = '')
    {
        if (empty($fields) || '*' == $fields) {
            // 获取全部视图字段
            $fields = [];
            foreach ($this->viewFields as $name => $val) {
                $k = isset($val['_as']) ? $val['_as'] : $name;
                $val = $this->_checkFields($name, $val);
                foreach ($val as $key => $field) {
                    if (is_numeric($key)) {
                        $fields[] = $k.'.'.$field.' AS '.$field;
                    } elseif ('_' != substr($key, 0, 1)) {
                        // 以_开头的为特殊定义
                        if (false !== strpos($key, '*') || false !== strpos($key, '(') || false !== strpos(
                                $key,
                                '.'
                            )
                        ) {
                            //如果包含* 或者 使用了sql方法 则不再添加前面的表名
                            $fields[] = $key.' AS '.$field;
                        } else {
                            $fields[] = $k.'.'.$key.' AS '.$field;
                        }
                    }
                }
            }
            $fields = implode(',', $fields);
        } else {
            if (!is_array($fields)) {
                $fields = explode(',', $fields);
            }
            // 解析成视图字段
            $array = [];
            foreach ($fields as $key => $field) {
                if (strpos($field, '(') || strpos(strtolower($field), ' as ')) {
                    // 使用了函数或者别名
                    $array[] = $field;
                    unset($fields[$key]);
                }
            }
            foreach ($this->viewFields as $name => $val) {
                $k = isset($val['_as']) ? $val['_as'] : $name;
                $val = $this->_checkFields($name, $val);
                foreach ($fields as $key => $field) {
                    if (false !== $_field = array_search($field, $val, true)) {
                        // 存在视图字段
                        if (is_numeric($_field)) {
                            $array[] = $k.'.'.$field.' AS '.$field;
                        } elseif ('_' != substr($_field, 0, 1)) {
                            //如果包含* 或者 使用了sql方法 则不再添加前面的表名
                            if (false !== strpos($_field, '*') || false !== strpos(
                                    $_field,
                                    '('
                                ) || false !== strpos($_field, '.')
                            ) {
                                $array[] = $_field.' AS '.$field;
                            } else {
                                $array[] = $k.'.'.$_field.' AS '.$field;
                            }
                        }
                    }
                }
            }
            $fields = implode(',', $array);
        }

        return $fields;
    }
protected function checkFields($fields = '')
    {
        if (empty($fields) || '*' == $fields) {
            // 获取全部视图字段
            $fields = [];
            foreach ($this->viewFields as $name => $val) {
                $k = isset($val['_as']) ? $val['_as'] : $name;
                $val = $this->_checkFields($name, $val);
                foreach ($val as $key => $field) {
                    if (is_numeric($key)) {
                        $fields[] = $k.'.'.$field.' AS '.$field;
                    } elseif ('_' != substr($key, 0, 1)) {
                        // 以_开头的为特殊定义
                        if (false !== strpos($key, '*') || false !== strpos($key, '(') || false !== strpos(
                                $key,
                                '.'
                            )
                        ) {
                            //如果包含* 或者 使用了sql方法 则不再添加前面的表名
                            $fields[] = $key.' AS '.$field;
                        } else {
                            $fields[] = $k.'.'.$key.' AS '.$field;
                        }
                    }
                }
            }
            $fields = implode(',', $fields);
        } else {
            if (!is_array($fields)) {
                $fields = explode(',', $fields);
            }
            // 解析成视图字段
            $array = [];
            foreach ($fields as $key => $field) {
                if (strpos($field, '(') || strpos(strtolower($field), ' as ')) {
                    // 使用了函数或者别名
                    $array[] = $field;
                    unset($fields[$key]);
                }
            }
            foreach ($this->viewFields as $name => $val) {
                $k = isset($val['_as']) ? $val['_as'] : $name;
                $val = $this->_checkFields($name, $val);
                foreach ($fields as $key => $field) {
                    if (false !== $_field = array_search($field, $val, true)) {
                        // 存在视图字段
                        if (is_numeric($_field)) {
                            $array[] = $k.'.'.$field.' AS '.$field;
                        } elseif ('_' != substr($_field, 0, 1)) {
                            //如果包含* 或者 使用了sql方法 则不再添加前面的表名
                            if (false !== strpos($_field, '*') || false !== strpos(
                                    $_field,
                                    '('
                                ) || false !== strpos($_field, '.')
                            ) {
                                $array[] = $_field.' AS '.$field;
                            } else {
                                $array[] = $k.'.'.$_field.' AS '.$field;
                            }
                        }
                    }
                }
            }
            $fields = implode(',', $array);
        }

        return $fields;
    }
检查fields表达式中的视图字段


@param string $fields 字段

@return string