From b1fa683e3d9cbb4efd6da5ab0bf5eaa35c88403a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 17:13:41 +0800 Subject: [PATCH] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0hidden=E5=92=8C?= =?UTF-8?q?visible=E6=96=B9=E6=B3=95=20=E7=94=A8=E4=BA=8E=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E8=BE=93=E5=87=BA=E7=9A=84=E5=AD=97=E6=AE=B5=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E5=92=8C=E6=98=BE=E7=A4=BA=20with=E5=92=8Crelation?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=E5=A4=9A=E6=AC=A1=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 59 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index b7288c93..d86834d6 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1503,6 +1503,30 @@ class Query 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 @@ -1911,7 +1935,11 @@ class Query } } $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; } @@ -1976,12 +2004,22 @@ class Query /** * 设置关联查询 * @access public - * @param string $relation 关联名称 + * @param string|array $relation 关联名称 * @return $this */ 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; } @@ -2295,6 +2333,7 @@ class Query /** @var Model $result */ $model = new $modelName($result); $model->isUpdate(true); + // 关联查询 if (!empty($options['relation'])) { $model->relationQuery($options['relation']); @@ -2303,6 +2342,14 @@ class Query if (!empty($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; } if (!empty($options['with'])) { @@ -2414,6 +2461,12 @@ class Query if (!empty($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'])) { $this->throwNotFound($options);