Query类增加hidden和visible方法 用于模型输出的字段隐藏和显示

with和relation方法支持多次调用
This commit is contained in:
thinkphp
2017-01-22 17:13:41 +08:00
parent 1b17858887
commit b1fa683e3d

View File

@@ -1503,6 +1503,30 @@ class Query
return $this; return $this;
} }
/**
* 设置模型输出显示字段
* @access public
* @param mixed $visible
* @return $this
*/
public function visible($visible)
{
$this->options['visible'] = $visible;
return $this;
}
/**
* 设置模型输出隐藏字段
* @access public
* @param mixed $hidden
* @return $this
*/
public function hidden($hidden)
{
$this->options['hidden'] = $hidden;
return $this;
}
/** /**
* 指定数据表别名 * 指定数据表别名
* @access public * @access public
@@ -1911,7 +1935,11 @@ class Query
} }
} }
$this->via(); $this->via();
$this->options['with'] = $with; if (isset($this->options['with'])) {
$this->options['with'] = array_merge($this->options['with'], $with);
} else {
$this->options['with'] = $with;
}
return $this; return $this;
} }
@@ -1976,12 +2004,22 @@ class Query
/** /**
* 设置关联查询 * 设置关联查询
* @access public * @access public
* @param string $relation 关联名称 * @param string|array $relation 关联名称
* @return $this * @return $this
*/ */
public function relation($relation) public function relation($relation)
{ {
$this->options['relation'] = $relation; if (empty($relation)) {
return $this;
}
if (is_string($relation)) {
$relation = explode(',', $relation);
}
if (isset($this->options['relation'])) {
$this->options['relation'] = array_mrege($this->options['relation'], $relation);
} else {
$this->options['relation'] = $relation;
}
return $this; return $this;
} }
@@ -2295,6 +2333,7 @@ class Query
/** @var Model $result */ /** @var Model $result */
$model = new $modelName($result); $model = new $modelName($result);
$model->isUpdate(true); $model->isUpdate(true);
// 关联查询 // 关联查询
if (!empty($options['relation'])) { if (!empty($options['relation'])) {
$model->relationQuery($options['relation']); $model->relationQuery($options['relation']);
@@ -2303,6 +2342,14 @@ class Query
if (!empty($options['with_count'])) { if (!empty($options['with_count'])) {
$model->relationCount($model, $options['with_count']); $model->relationCount($model, $options['with_count']);
} }
// 显式或隐藏属性
if (!empty($options['visible'])) {
$model->visible($options['visible']);
} elseif (!empty($options['hidden'])) {
$model->hidden($options['hidden']);
}
$resultSet[$key] = $model; $resultSet[$key] = $model;
} }
if (!empty($options['with'])) { if (!empty($options['with'])) {
@@ -2414,6 +2461,12 @@ class Query
if (!empty($options['with_count'])) { if (!empty($options['with_count'])) {
$data->relationCount($data, $options['with_count']); $data->relationCount($data, $options['with_count']);
} }
// 显式或隐藏属性
if (!empty($options['visible'])) {
$data->visible($options['visible']);
} elseif (!empty($options['hidden'])) {
$data->hidden($options['hidden']);
}
} }
} elseif (!empty($options['fail'])) { } elseif (!empty($options['fail'])) {
$this->throwNotFound($options); $this->throwNotFound($options);