From 6152a41665e9ecf9573647405ffffd2cc220f21a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 18 Apr 2016 22:05:48 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E6=B7=BB=E5=8A=A0field=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E5=86=99=E5=85=A5=E7=9A=84=E5=AD=97=E6=AE=B5=20?= =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E7=9A=84field=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 23 +++++++++++++++++++++++ library/think/db/Query.php | 3 +++ 2 files changed, 26 insertions(+) diff --git a/library/think/Model.php b/library/think/Model.php index a4519732..43def959 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -40,6 +40,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 字段验证规则 protected $validate; + // 字段属性 + protected $field = []; // 数据信息 protected $data = []; // 缓存数据 @@ -343,6 +345,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } + // 检测字段 + if (!empty($this->field)) { + foreach ($data as $key => $val) { + if (!in_array($key, $this->field)) { + unset($data[$key]); + } + } + } + // 数据自动完成 $this->autoCompleteData($this->auto); @@ -400,6 +411,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $result; } + /** + * 设置允许写入的字段 + * @access public + * @param bool $update + * @return $this + */ + public function field($field) + { + $this->field = $field; + return $this; + } + /** * 是否为更新数据 * @access public diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 216fa440..116fcbe6 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -367,6 +367,9 @@ class Query */ public function field($field, $except = false, $tableName = '', $prefix = '', $alias = '') { + if (empty($field)) { + return $this; + } if (is_string($field)) { $field = explode(',', $field); }