改进Model类的修改器机制 增加 setAttr和 getAttr方法

This commit is contained in:
thinkphp
2016-06-03 17:33:52 +08:00
parent bd498a587e
commit fa0e85983c
3 changed files with 203 additions and 176 deletions

View File

@@ -150,24 +150,25 @@ class Merge extends Model
public function save($data = [], $where = [], $getId = true)
{
if (!empty($data)) {
// 数据自动验证
if (!$this->validateData()) {
return false;
}
// 数据对象赋值
foreach ($data as $key => $value) {
$this->__set($key, $value);
$this->setAttr($key, $value);
}
if (!empty($where)) {
$this->isUpdate = true;
}
}
// 数据自动验证
if (!$this->validateData()) {
return false;
}
// 数据自动完成
$this->autoCompleteData($this->auto);
// 自动写入更新时间
if ($this->autoWriteTimestamp && $this->updateTime) {
$this->__set($this->updateTime, null);
$this->setAttr($this->updateTime, null);
}
$db = $this->db();
@@ -203,7 +204,7 @@ class Merge extends Model
// 自动写入创建时间
if ($this->autoWriteTimestamp && $this->createTime) {
$this->__set($this->createTime, null);
$this->setAttr($this->createTime, null);
}
if (false === $this->trigger('before_insert', $this)) {

View File

@@ -177,7 +177,7 @@ class Relation
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
}
$result->__set($relation, $this->resultSetBuild($data[$result->$localKey], $class));
$result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class));
}
}
break;
@@ -206,7 +206,7 @@ class Relation
$data[$result->$pk] = [];
}
$result->__set($relation, $this->resultSetBuild($data[$result->$pk], $class));
$result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class));
}
}
break;
@@ -266,7 +266,7 @@ class Relation
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
}
$result->__set($relation, $this->resultSetBuild($data[$result->$localKey], $class));
$result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class));
}
break;
case self::BELONGS_TO_MANY:
@@ -280,7 +280,7 @@ class Relation
if (!isset($data[$pk])) {
$data[$pk] = [];
}
$result->__set($relation, $this->resultSetBuild($data[$pk], $class));
$result->setAttr($relation, $this->resultSetBuild($data[$pk], $class));
}
break;
@@ -315,7 +315,7 @@ class Relation
// 设置关联模型属性
$list[$modelName] = [];
}
$result->__set($relation, new $model($list[$modelName]));
$result->setAttr($relation, new $model($list[$modelName]));
}
/**