From d2b75581fb3829bfbd6d7e91e3272fb0fbc8421c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 18 Sep 2016 20:12:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=B1=BB=E7=9A=84field?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E5=AE=9A=E4=B9=89=E7=AE=80=E5=8C=96=20?= =?UTF-8?q?=E5=8F=96=E6=B6=88Query=E7=B1=BB=E7=9A=84allowField=E5=92=8Cset?= =?UTF-8?q?FieldType=E6=96=B9=E6=B3=95=E5=8F=8A=E7=9B=B8=E5=85=B3=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=20=E4=BA=A4=E7=BB=99=E6=95=B0=E6=8D=AE=E8=A1=A8?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 24 ++------------------- library/think/db/Query.php | 44 ++++---------------------------------- 2 files changed, 6 insertions(+), 62 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 3966969e..30b71d52 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -156,24 +156,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $query->name($this->name); } - if (!empty($this->field)) { - if (true === $this->field) { - $type = $query->getTableInfo('', 'type'); - } else { - $type = []; - foreach ((array) $this->field as $key => $val) { - if (is_int($key)) { - $key = $val; - $val = 'varchar'; - } - $type[$key] = $val; - } - } - $query->setFieldType($type); - $this->field = array_keys($type); - $query->allowField($this->field); - } - if (!empty($this->pk)) { $query->pk($this->pk); } @@ -632,7 +614,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 检测字段 if (!empty($this->field)) { foreach ($this->data as $key => $val) { - if (!in_array($key, $this->field) && !array_key_exists($key, $this->field)) { + if (!in_array($key, $this->field)) { unset($this->data[$key]); } } @@ -780,9 +762,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function allowField($field) { if (true === $field) { - $field = $this->db()->getTableInfo('', 'type'); - $this->db()->setFieldType($field); - $field = array_keys($field); + $field = $this->db()->getTableInfo('', 'fields'); } $this->field = $field; return $this; diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 238523d6..04a8184c 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -43,10 +43,6 @@ class Query protected $name = ''; // 当前数据表主键 protected $pk; - // 当前表字段类型信息 - protected $fieldType = []; - // 当前允许的字段列表 - protected $allowField = []; // 当前数据表前缀 protected $prefix = ''; // 查询参数 @@ -741,11 +737,11 @@ class Query } if (true === $field) { // 获取全部字段 - $fields = !empty($this->allowField) && ('' == $tableName || $this->getTable() == $tableName) ? $this->allowField : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); + $fields = $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); $field = $fields ?: ['*']; } elseif ($except) { // 字段排除 - $fields = !empty($this->allowField) && ('' == $tableName || $this->getTable() == $tableName) ? $this->allowField : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); + $fields = $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); $field = $fields ? array_diff($fields, $field) : $field; } if ($tableName) { @@ -1260,35 +1256,6 @@ class Query return $this; } - /** - * 设置数据表字段 - * @access public - * @param string|array $field 字段信息 - * @return $this - */ - public function allowField($field) - { - if (true === $field) { - $field = $this->getTableInfo('', 'fields'); - } elseif (is_string($field)) { - $field = explode(',', $field); - } - $this->allowField = $field; - return $this; - } - - /** - * 设置字段类型 - * @access public - * @param array $fieldType 字段类型信息 - * @return $this - */ - public function setFieldType($fieldType = []) - { - $this->fieldType = $fieldType; - return $this; - } - /** * 指定数据表主键 * @access public @@ -1421,13 +1388,13 @@ class Query // 获取当前数据表字段信息 public function getTableFields($options) { - return !empty($this->allowField) ? $this->allowField : $this->getTableInfo($options['table'], 'fields'); + return $this->getTableInfo($options['table'], 'fields'); } // 获取当前数据表字段类型 public function getFieldsType($options) { - return !empty($this->fieldType) ? $this->fieldType : $this->getTableInfo($options['table'], 'type'); + return $this->getTableInfo($options['table'], 'type'); } // 获取当前数据表绑定信息 @@ -2003,9 +1970,6 @@ class Query $model = $this->model; $data = new $model($data); $data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); - if ($this->allowField) { - $data->allowField($this->allowField); - } // 关联查询 if (!empty($options['relation'])) { $data->relationQuery($options['relation']);