From a1edf89aecfa9746383913df2671616571bd35bb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 10 Aug 2016 15:47:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=20Model=E7=B1=BB=E7=9A=84fie?= =?UTF-8?q?ldType=E5=B1=9E=E6=80=A7=E5=AE=9A=E4=B9=89=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=94=B1field=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index e8702b9e..ce5c9017 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -56,12 +56,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $error; // 字段验证规则 protected $validate; - // 数据表主键 复合主键使用数组定义 + // 数据表主键 复合主键使用数组定义 不设置则自动获取 protected $pk; - // 字段属性 + // 数据表字段信息 留空则自动获取 protected $field = []; - // 字段类型 - protected $fieldType = []; // 显示属性 protected $visible = []; // 隐藏属性 @@ -155,13 +153,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } if (!empty($this->field)) { + if (true === $this->field) { + $this->field = $this->db()->getTableInfo('', 'type'); + } + if (is_array($this->field) && key($this->field) !== 0) { + $query->setFieldType($this->field); + $this->field = array_keys($this->field); + } $query->allowField($this->field); } - if (!empty($this->fieldType)) { - $query->setFieldType($this->fieldType); - } - if (!empty($this->pk)) { $query->pk($this->pk); } @@ -616,9 +617,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 检测字段 if (!empty($this->field)) { - if (true === $this->field) { - $this->field = $this->db()->getTableInfo('', 'fields'); - } foreach ($this->data as $key => $val) { if (!in_array($key, $this->field)) { unset($this->data[$key]); @@ -741,6 +739,11 @@ 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); + } $this->field = $field; return $this; }