Model类添加field方法 用于设置允许写入的字段 改进Query类的field方法

This commit is contained in:
thinkphp
2016-04-18 22:05:48 +08:00
parent 9152c5ec0c
commit 6152a41665
2 changed files with 26 additions and 0 deletions

View File

@@ -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

View File

@@ -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);
}