更新内核

增加自定义表单(未完成)
This commit is contained in:
2017-07-04 12:13:24 +08:00
parent 8af311aa07
commit aef819d417
82 changed files with 12328 additions and 59 deletions

View File

@@ -838,6 +838,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return json_encode($this->toArray(), $options);
}
/**
* 移除当前模型的关联属性
* @access public
* @return $this
*/
public function removeRelation()
{
$this->relation = [];
return $this;
}
/**
* 转换当前模型数据集为数据集对象
* @access public
@@ -964,9 +975,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
$pk = $this->getPk();
if ($this->isUpdate) {
// 检测字段
$this->checkAllowField($this->data, array_merge($this->auto, $this->update));
// 自动更新
$this->autoCompleteData($this->update);
@@ -986,7 +994,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return 0;
} elseif ($this->autoWriteTimestamp && $this->updateTime && !isset($data[$this->updateTime])) {
// 自动写入更新时间
$data[$this->updateTime] = $this->autoWriteTimestamp($this->updateTime);
$data[$this->updateTime] = $this->autoWriteTimestamp($this->updateTime);
$this->data[$this->updateTime] = $data[$this->updateTime];
}
if (empty($where) && !empty($this->updateWhere)) {
@@ -1008,8 +1017,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
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)) {
@@ -1020,9 +1036,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->trigger('after_update', $this);
} else {
// 检测字段
$this->checkAllowField($this->data, array_merge($this->auto, $this->insert));
// 自动写入
$this->autoCompleteData($this->insert);
@@ -1040,7 +1053,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
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])) {
@@ -1073,22 +1092,22 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $result;
}
protected function checkAllowField(&$data, $auto = [])
protected function checkAllowField($auto = [])
{
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');
$field = $this->field;
} else {
$field = array_merge($this->field, $auto);
}
foreach ($data as $key => $val) {
if (!in_array($key, $field)) {
unset($data[$key]);
}
}
} else {
$field = [];
}
return $field;
}
protected function autoRelationUpdate($relation)
@@ -1627,6 +1646,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = new static();
$query = $model->db();
$params = func_get_args();
array_shift($params);
array_unshift($params, $query);
if ($name instanceof \Closure) {
call_user_func_array($name, $params);
@@ -1927,7 +1947,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 记录当前关联信息
$model = $this->parseModel($model);
$name = Loader::parseName(basename(str_replace('\\', '/', $model)));
$table = $table ?: $this->getQuery()->getTable(Loader::parseName($this->name) . '_' . $name);
$table = $table ?: Loader::parseName($this->name) . '_' . $name;
$foreignKey = $foreignKey ?: $name . '_id';
$localKey = $localKey ?: $this->getForeignKey($this->name);
return new BelongsToMany($this, $model, $table, $foreignKey, $localKey);