mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 15:02:47 +08:00
模型数据和关联数据分离
This commit is contained in:
@@ -68,6 +68,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $data = [];
|
protected $data = [];
|
||||||
// 原始数据
|
// 原始数据
|
||||||
protected $origin = [];
|
protected $origin = [];
|
||||||
|
// 关联模型
|
||||||
|
protected $relation = [];
|
||||||
|
|
||||||
// 保存自动完成列表
|
// 保存自动完成列表
|
||||||
protected $auto = [];
|
protected $auto = [];
|
||||||
@@ -89,8 +91,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $isUpdate = false;
|
protected $isUpdate = false;
|
||||||
// 更新条件
|
// 更新条件
|
||||||
protected $updateWhere;
|
protected $updateWhere;
|
||||||
// 当前执行的关联对象
|
|
||||||
protected $relation;
|
|
||||||
// 验证失败是否抛出异常
|
// 验证失败是否抛出异常
|
||||||
protected $failException = false;
|
protected $failException = false;
|
||||||
// 全局查询范围
|
// 全局查询范围
|
||||||
@@ -318,6 +318,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return $this->data;
|
return $this->data;
|
||||||
} elseif (array_key_exists($name, $this->data)) {
|
} elseif (array_key_exists($name, $this->data)) {
|
||||||
return $this->data[$name];
|
return $this->data[$name];
|
||||||
|
} elseif (array_key_exists($name, $this->relation)) {
|
||||||
|
return $this->relation[$name];
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name);
|
throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name);
|
||||||
}
|
}
|
||||||
@@ -341,14 +343,22 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
|
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
|
||||||
if (method_exists($this, $method)) {
|
if (method_exists($this, $method)) {
|
||||||
$value = $this->$method($value, array_merge($this->data, $data));
|
$value = $this->$method($value, array_merge($this->data, $data));
|
||||||
|
}
|
||||||
|
if ($this->isRelationAttr($name)) {
|
||||||
|
$isRelationData = true;
|
||||||
} elseif (isset($this->type[$name])) {
|
} elseif (isset($this->type[$name])) {
|
||||||
// 类型转换
|
// 类型转换
|
||||||
$value = $this->writeTransform($value, $this->type[$name]);
|
$value = $this->writeTransform($value, $this->type[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置数据对象属性
|
// 设置数据对象属性
|
||||||
$this->data[$name] = $value;
|
if (isset($isRelationData)) {
|
||||||
|
$this->relation[$name] = $value;
|
||||||
|
} else {
|
||||||
|
$this->data[$name] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -506,13 +516,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$value = $this->formatDateTime($value, $this->dateFormat);
|
$value = $this->formatDateTime($value, $this->dateFormat);
|
||||||
}
|
}
|
||||||
} elseif ($notFound) {
|
} elseif ($notFound) {
|
||||||
$method = Loader::parseName($name, 1, false);
|
$relation = $this->isRelationAttr($name);
|
||||||
if (method_exists($this, $method) && $this->$method() instanceof Relation) {
|
$method = Loader::parseName($name, 1, false);
|
||||||
$modelRelation = $this->$method();
|
if ($relation) {
|
||||||
|
$modelRelation = $this->$relation();
|
||||||
// 不存在该字段 获取关联数据
|
// 不存在该字段 获取关联数据
|
||||||
$value = $this->getRelationData($modelRelation);
|
$value = $this->getRelationData($modelRelation);
|
||||||
// 保存关联对象值
|
// 保存关联对象值
|
||||||
$this->data[$name] = $value;
|
$this->relation[$name] = $value;
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name);
|
throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name);
|
||||||
}
|
}
|
||||||
@@ -890,11 +901,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
unset($this->data[$val]);
|
unset($this->data[$val]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif (isset($this->data[$name])) {
|
} elseif (isset($this->relation[$name])) {
|
||||||
$relation[$name] = $this->data[$name];
|
$relation[$name] = $this->relation[$name];
|
||||||
if (!$this->isUpdate) {
|
|
||||||
unset($this->data[$name]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -958,15 +966,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
unset($data[$pk]);
|
unset($data[$pk]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关联更新
|
|
||||||
if (isset($relation)) {
|
|
||||||
foreach ($relation as $name => $val) {
|
|
||||||
if (isset($data[$name])) {
|
|
||||||
unset($data[$name]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 模型更新
|
// 模型更新
|
||||||
$result = $this->db()->where($where)->update($data);
|
$result = $this->db()->where($where)->update($data);
|
||||||
|
|
||||||
@@ -1061,6 +1060,23 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查属性是否为关联属性 如果是则返回关联方法名
|
||||||
|
* @access public
|
||||||
|
* @param string $attr 关联属性名
|
||||||
|
* @return string|false
|
||||||
|
*/
|
||||||
|
protected function isRelationAttr($attr)
|
||||||
|
{
|
||||||
|
$relation = Loader::parseName($attr, 1, false);
|
||||||
|
|
||||||
|
if (method_exists($this, $relation) && $this->$relation() instanceof Relation) {
|
||||||
|
return $relation;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存多个数据到当前数据对象
|
* 保存多个数据到当前数据对象
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
Reference in New Issue
Block a user