改进模型类的checkAllowField方法

This commit is contained in:
thinkphp
2017-06-28 23:37:29 +08:00
parent a27f5299df
commit aaeba8a565

View File

@@ -985,8 +985,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 获取有更新的数据 // 获取有更新的数据
$data = $this->getChangedData(); $data = $this->getChangedData();
// 检测字段
$this->checkAllowField($data, array_merge($this->auto, $this->update));
if (empty($data) || (count($data) == 1 && is_string($pk) && isset($data[$pk]))) { if (empty($data) || (count($data) == 1 && is_string($pk) && isset($data[$pk]))) {
// 关联更新 // 关联更新
@@ -1019,8 +1017,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
unset($data[$pk]); unset($data[$pk]);
} }
// 检测字段
$allowFields = $this->checkAllowField(array_merge($this->auto, $this->update));
// 模型更新 // 模型更新
$result = $this->getQuery()->where($where)->update($data); if (!empty($allowFields)) {
$result = $this->getQuery()->where($where)->strict(false)->field($allowFields)->update($data);
} else {
$result = $this->getQuery()->where($where)->update($data);
}
// 关联更新 // 关联更新
if (isset($relation)) { if (isset($relation)) {
@@ -1031,9 +1036,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->trigger('after_update', $this); $this->trigger('after_update', $this);
} else { } else {
// 检测字段
$this->checkAllowField($this->data, array_merge($this->auto, $this->insert));
// 自动写入 // 自动写入
$this->autoCompleteData($this->insert); $this->autoCompleteData($this->insert);
@@ -1051,7 +1053,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false; return false;
} }
$result = $this->getQuery()->insert($this->data); // 检测字段
$allowFields = $this->checkAllowField(array_merge($this->auto, $this->insert));
if (!empty($allowFields)) {
$result = $this->getQuery()->strict(false)->field($allowFields)->insert($this->data);
} else {
$result = $this->getQuery()->insert($this->data);
}
// 获取自动增长主键 // 获取自动增长主键
if ($result && is_string($pk) && (!isset($this->data[$pk]) || '' == $this->data[$pk])) { if ($result && is_string($pk) && (!isset($this->data[$pk]) || '' == $this->data[$pk])) {
@@ -1084,22 +1092,22 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $result; return $result;
} }
protected function checkAllowField(&$data, $auto = []) protected function checkAllowField($auto = [])
{ {
if (!empty($this->field)) { if (!empty($this->field)) {
if (true === $this->field) { if (!empty($this->origin)) {
$this->field = array_keys($this->origin);
$field = $this->field;
} elseif (true === $this->field) {
$this->field = $this->getQuery()->getTableInfo('', 'fields'); $this->field = $this->getQuery()->getTableInfo('', 'fields');
$field = $this->field; $field = $this->field;
} else { } else {
$field = array_merge($this->field, $auto); $field = array_merge($this->field, $auto);
} }
} else {
foreach ($data as $key => $val) { $field = [];
if (!in_array($key, $field)) {
unset($data[$key]);
}
}
} }
return $field;
} }
protected function autoRelationUpdate($relation) protected function autoRelationUpdate($relation)