From 7b5044b888c1e2f0610fb315f7d68ee82308a621 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Oct 2017 16:25:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=92=E9=99=A4=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=96=B9=E6=B3=95except=20=E5=BA=9F=E5=BC=83=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=BD=BF=E7=94=A8disuse=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 | 47 +++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index c7d9a39c..d7f91db0 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -59,6 +59,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $field = []; // 数据排除字段 protected $except = []; + // 数据废弃字段 + protected $disuse = []; // 只读字段 protected $readonly = []; // 显示属性 @@ -124,6 +126,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } else { $this->data = $data; } + + if ($this->disuse) { + // 废弃字段 + foreach ((array) $this->disuse as $key) { + if (isset($this->data[$key])) { + unset($this->data[$key]); + } + } + } + // 记录原始数据 $this->origin = $this->data; @@ -1137,12 +1149,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($this->autoWriteTimestamp) { array_push($field, $this->createTime, $this->updateTime); } + } elseif (!empty($this->except)) { + $fields = $this->getQuery()->getTableInfo('', 'fields'); + $field = array_diff($fields, (array) $this->except); + $this->field = $field; } else { $field = []; } - if ($this->except) { - // 排除字段 - $field = array_diff($field, (array) $this->except); + + if ($this->disuse) { + // 废弃字段 + $field = array_diff($field, (array) $this->disuse); } return $field; } @@ -1297,7 +1314,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 设置允许写入的字段 * @access public - * @param mixed $field 允许写入的字段 如果为true只允许写入数据表字段 + * @param string|array $field 允许写入的字段 如果为true只允许写入数据表字段 * @return $this */ public function allowField($field) @@ -1309,6 +1326,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $this; } + /** + * 设置排除写入的字段 + * @access public + * @param string|array $field 排除允许写入的字段 + * @return $this + */ + public function except($field) + { + if (is_string($field)) { + $field = explode(',', $field); + } + $this->except = $field; + return $this; + } + /** * 设置只读字段 * @access public @@ -2066,9 +2098,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function __call($method, $args) { $query = $this->db(true, false); - if ($this->except) { - $query->field($this->except, true); - } if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method; @@ -2084,10 +2113,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $model = new static(); $query = $model->db(); - if ($model->except) { - $query->field($model->except, true); - } - if (method_exists($model, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method;