mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
修正Model类order方法一处bug,优化改进视图模型
This commit is contained in:
@@ -1730,8 +1730,8 @@ class Model
|
|||||||
public function order($field, $order = null)
|
public function order($field, $order = null)
|
||||||
{
|
{
|
||||||
if (!empty($field)) {
|
if (!empty($field)) {
|
||||||
if (!is_array($field)) {
|
if (is_string($field)) {
|
||||||
$field = empty($order) ? [$field] : [(string) $field => (string) $order];
|
$field = empty($order) ? $field : [$field => $order];
|
||||||
}
|
}
|
||||||
$this->options['order'] = $field;
|
$this->options['order'] = $field;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,28 +31,31 @@ trait View
|
|||||||
{
|
{
|
||||||
if (empty($this->trueTableName)) {
|
if (empty($this->trueTableName)) {
|
||||||
$tableName = '';
|
$tableName = '';
|
||||||
foreach ($this->viewFields as $key => $view) {
|
$len = 0;
|
||||||
|
foreach ($this->viewFields as $name => $view) {
|
||||||
// 获取数据表名称
|
// 获取数据表名称
|
||||||
if (isset($view['_table'])) {
|
if (isset($view['_table'])) {
|
||||||
// 2011/10/17 添加实际表名定义支持 可以实现同一个表的视图
|
// 2011/10/17 添加实际表名定义支持 可以实现同一个表的视图
|
||||||
$tableName .= $view['_table'];
|
$tableName .= $view['_table'];
|
||||||
} else {
|
} else {
|
||||||
$class = $key . 'Model';
|
$tableName .= \think\Loader::model($name)->getTableName();
|
||||||
$model = class_exists($class) ? new $class() : M($key);
|
|
||||||
$tableName .= $model->getTableName();
|
|
||||||
}
|
}
|
||||||
// 表别名定义
|
// 表别名定义
|
||||||
$tableName .= !empty($view['_as']) ? ' ' . $view['_as'] : ' ' . $key;
|
$tableName .= !empty($view['_as']) ? ' ' . $view['_as'] : ' ' . $name;
|
||||||
// 支持ON 条件定义
|
// 支持ON 条件定义
|
||||||
$tableName .= !empty($view['_on']) ? ' ON ' . $view['_on'] : '';
|
$tableName .= !empty($view['_on']) ? ' ON ' . $view['_on'] : '';
|
||||||
|
if (!empty($view['_type'])) {
|
||||||
// 指定JOIN类型 例如 RIGHT INNER LEFT 下一个表有效
|
// 指定JOIN类型 例如 RIGHT INNER LEFT 下一个表有效
|
||||||
$type = !empty($view['_type']) ? $view['_type'] : '';
|
$type = strtoupper($view['_type']);
|
||||||
$tableName .= ' ' . strtoupper($type) . ' JOIN ';
|
$tableName .= ' ' . $type . ' JOIN ';
|
||||||
$len = strlen($type . '_JOIN ');
|
if ($view == end($this->viewFields)) {
|
||||||
|
$len = strlen($type) + 7;
|
||||||
}
|
}
|
||||||
$tableName = substr($tableName, 0, -$len);
|
|
||||||
$this->trueTableName = $tableName;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
$this->trueTableName = $len ? substr($tableName, 0, -$len) : $tableName;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->trueTableName;
|
return $this->trueTableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,19 +67,36 @@ trait View
|
|||||||
*/
|
*/
|
||||||
protected function _options_filter(&$options)
|
protected function _options_filter(&$options)
|
||||||
{
|
{
|
||||||
if (isset($options['field'])) {
|
$viewFields = [];
|
||||||
$options['field'] = $this->checkFields($options['field']);
|
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)) {
|
||||||
|
$viewFields[$k . '.' . $field] = $field;
|
||||||
|
} elseif ('_' != substr($key, 0, 1)) {
|
||||||
|
// 以_开头的为特殊定义
|
||||||
|
if (false !== strpos($key, '*') || false !== strpos($key, '(') || false !== strpos($key, '.')) {
|
||||||
|
//如果包含* 或者 使用了sql方法 则不再添加前面的表名
|
||||||
|
$viewFields[$key] = $field;
|
||||||
} else {
|
} else {
|
||||||
$options['field'] = $this->checkFields();
|
$viewFields[$k . '.' . $key] = $field;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (empty($options['field'])) {
|
||||||
|
$options['field'] = '*';
|
||||||
|
}
|
||||||
|
$options['field'] = $this->checkFields($options['field'], $viewFields);
|
||||||
if (isset($options['group'])) {
|
if (isset($options['group'])) {
|
||||||
$options['group'] = $this->checkGroup($options['group']);
|
$options['group'] = $this->checkGroup($options['group'], $viewFields);
|
||||||
}
|
}
|
||||||
if (isset($options['where'])) {
|
if (isset($options['where'])) {
|
||||||
$options['where'] = $this->checkCondition($options['where']);
|
$options['where'] = $this->checkCondition($options['where'], $viewFields);
|
||||||
}
|
}
|
||||||
if (isset($options['order'])) {
|
if (isset($options['order'])) {
|
||||||
$options['order'] = $this->checkOrder($options['order']);
|
$options['order'] = $this->checkOrder($options['order'], $viewFields);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,6 +114,7 @@ trait View
|
|||||||
$fields = array_merge($fields, \think\Loader::model($name)->getFields());
|
$fields = array_merge($fields, \think\Loader::model($name)->getFields());
|
||||||
unset($fields[$pos]);
|
unset($fields[$pos]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,154 +122,135 @@ trait View
|
|||||||
* 检查条件中的视图字段
|
* 检查条件中的视图字段
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param mixed $data 条件表达式
|
* @param mixed $data 条件表达式
|
||||||
|
* @param array $fields 视图字段数组
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function checkCondition($where)
|
protected function checkCondition($where, $viewFields)
|
||||||
{
|
{
|
||||||
if (is_array($where)) {
|
if (is_array($where)) {
|
||||||
$view = [];
|
$_where = [];
|
||||||
// 检查视图字段
|
foreach ($where as $field => $value) {
|
||||||
foreach ($this->viewFields as $key => $val) {
|
if (false !== $k = array_search($field, $viewFields, true)) {
|
||||||
$k = isset($val['_as']) ? $val['_as'] : $key;
|
|
||||||
$val = $this->_checkFields($key, $val);
|
|
||||||
foreach ($where as $name => $value) {
|
|
||||||
if (false !== $field = array_search($name, $val, true)) {
|
|
||||||
// 存在视图字段
|
// 存在视图字段
|
||||||
$_key = is_numeric($field) ? $k . '.' . $name : $k . '.' . $field;
|
$_where[$k] = $value;
|
||||||
$view[$_key] = $value;
|
} else {
|
||||||
unset($where[$name]);
|
$_where[$field] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return $_where;
|
||||||
$where = array_merge($where, $view);
|
} else {
|
||||||
}
|
|
||||||
return $where;
|
return $where;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查Order表达式中的视图字段
|
* 检查Order表达式中的视图字段
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $order 字段
|
* @param string|array $order 字段
|
||||||
|
* @param array $fields 视图字段数组
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function checkOrder($order = '')
|
protected function checkOrder($order = '', $viewFields)
|
||||||
{
|
{
|
||||||
if (is_string($order) && !empty($order)) {
|
if (empty($order)) {
|
||||||
$orders = explode(',', $order);
|
return '';
|
||||||
|
} elseif (is_string($order)) {
|
||||||
|
$order = explode(',', $order);
|
||||||
|
}
|
||||||
$_order = [];
|
$_order = [];
|
||||||
foreach ($orders as $order) {
|
foreach ($order as $key => $field) {
|
||||||
$array = explode(' ', $order);
|
if (is_numeric($key)) {
|
||||||
$field = $array[0];
|
if ($pos = strpos($field, ' ')) {
|
||||||
$sort = isset($array[1]) ? $array[1] : 'ASC';
|
$sort = substr($field, $pos);
|
||||||
// 解析成视图字段
|
$field = substr($field, 0, $pos);
|
||||||
foreach ($this->viewFields as $name => $val) {
|
} else {
|
||||||
$k = isset($val['_as']) ? $val['_as'] : $name;
|
$sort = '';
|
||||||
$val = $this->_checkFields($name, $val);
|
}
|
||||||
if (false !== $_field = array_search($field, $val, true)) {
|
} else {
|
||||||
|
$sort = ' ' . $field;
|
||||||
|
$field = $key;
|
||||||
|
}
|
||||||
|
if (false !== $k = array_search($field, $viewFields, true)) {
|
||||||
// 存在视图字段
|
// 存在视图字段
|
||||||
$field = is_numeric($_field) ? $k . '.' . $field : $k . '.' . $_field;
|
$field = $k . $sort;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
$_order[] = $field;
|
||||||
}
|
}
|
||||||
$_order[] = $field . ' ' . $sort;
|
|
||||||
}
|
return $_order;
|
||||||
$order = implode(',', $_order);
|
|
||||||
}
|
|
||||||
return $order;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查Group表达式中的视图字段
|
* 检查Group表达式中的视图字段
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $group 字段
|
* @param string $group 字段
|
||||||
|
* @param array $fields 视图字段数组
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function checkGroup($group = '')
|
protected function checkGroup($group = '', $viewFields)
|
||||||
{
|
{
|
||||||
if (!empty($group)) {
|
if (empty($group)) {
|
||||||
$groups = explode(',', $group);
|
return '';
|
||||||
$_group = [];
|
} elseif (is_string($group)) {
|
||||||
foreach ($groups as $field) {
|
$group = explode(',', $group);
|
||||||
// 解析成视图字段
|
}
|
||||||
foreach ($this->viewFields as $name => $val) {
|
foreach ($group as &$field) {
|
||||||
$k = isset($val['_as']) ? $val['_as'] : $name;
|
if (false !== $k = array_search($field, $viewFields, true)) {
|
||||||
$val = $this->_checkFields($name, $val);
|
|
||||||
if (false !== $_field = array_search($field, $val, true)) {
|
|
||||||
// 存在视图字段
|
// 存在视图字段
|
||||||
$field = is_numeric($_field) ? $k . '.' . $field : $k . '.' . $_field;
|
$field = $k;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$_group[] = $field;
|
|
||||||
}
|
return implode(',', $group);
|
||||||
$group = implode(',', $_group);
|
|
||||||
}
|
|
||||||
return $group;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查fields表达式中的视图字段
|
* 检查fields表达式中的视图字段
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $fields 字段
|
* @param string $fields 字段
|
||||||
|
* @param array $fields 视图字段数组
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function checkFields($fields = '')
|
protected function checkFields($fields = '*', $viewFields)
|
||||||
{
|
{
|
||||||
if (empty($fields) || '*' == $fields) {
|
var_dump($fields);
|
||||||
// 获取全部视图字段
|
if (is_string($fields)) {
|
||||||
$fields = [];
|
if ('*' == $fields) {
|
||||||
foreach ($this->viewFields as $name => $val) {
|
return $viewFields;
|
||||||
$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 {
|
} else {
|
||||||
$fields[] = $k . '.' . $key . ' AS ' . $field;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$fields = implode(',', $fields);
|
|
||||||
} else {
|
|
||||||
if (!is_array($fields)) {
|
|
||||||
$fields = explode(',', $fields);
|
$fields = explode(',', $fields);
|
||||||
}
|
}
|
||||||
// 解析成视图字段
|
|
||||||
$array = [];
|
|
||||||
foreach ($fields as $key => $field) {
|
|
||||||
if (strpos($field, '(') || strpos(strtolower($field), ' as ')) {
|
|
||||||
// 使用了函数或者别名
|
|
||||||
$array[] = $field;
|
|
||||||
unset($fields[$key]);
|
|
||||||
}
|
}
|
||||||
}
|
$_fields = [];
|
||||||
foreach ($this->viewFields as $name => $val) {
|
|
||||||
$k = isset($val['_as']) ? $val['_as'] : $name;
|
|
||||||
$val = $this->_checkFields($name, $val);
|
|
||||||
foreach ($fields as $key => $field) {
|
foreach ($fields as $key => $field) {
|
||||||
if (false !== $_field = array_search($field, $val, true)) {
|
if (is_numeric($key)) {
|
||||||
// 存在视图字段
|
if ($pos = strpos($field, ' ')) {
|
||||||
if (is_numeric($_field)) {
|
$alias = substr($field, $pos);
|
||||||
$array[] = $k . '.' . $field . ' AS ' . $field;
|
$field = substr($field, 0, $pos);
|
||||||
} elseif ('_' != substr($_field, 0, 1)) {
|
|
||||||
if (false !== strpos($_field, '*') || false !== strpos($_field, '(') || false !== strpos($_field, '.')) {
|
|
||||||
//如果包含* 或者 使用了sql方法 则不再添加前面的表名
|
|
||||||
$array[] = $_field . ' AS ' . $field;
|
|
||||||
} else {
|
} else {
|
||||||
$array[] = $k . '.' . $_field . ' AS ' . $field;
|
$alias = ' AS ' . $field;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$alias = ' AS ' . $field;
|
||||||
|
$field = $key;
|
||||||
|
}
|
||||||
|
if (strpos($field, '(')) {
|
||||||
|
if (preg_match_all('/(?<!\s)\b(\w+)\b(?![\.\(])/', $field, $matches, PREG_OFFSET_CAPTURE)) {
|
||||||
|
// 倒序找出的字段数组,以便从后向前替换
|
||||||
|
$items = array_reverse($matches[0]);
|
||||||
|
foreach ($items as $item) {
|
||||||
|
if (false !== $k = array_search($item[0], $viewFields, true)) {
|
||||||
|
$field = substr_replace($field, $k, $item[1], strlen($item[0]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} elseif (false !== $k = array_search($field, $viewFields, true)) {
|
||||||
}
|
// 存在视图字段
|
||||||
$fields = implode(',', $array);
|
$field = $k . $alias;
|
||||||
}
|
}
|
||||||
return $fields;
|
$_fields[] = $field;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $_fields;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user