mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
Model类增加一对一关联自动写入机制
This commit is contained in:
@@ -107,6 +107,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $batchValidate = false;
|
protected $batchValidate = false;
|
||||||
// 查询数据集对象
|
// 查询数据集对象
|
||||||
protected $resultSetType;
|
protected $resultSetType;
|
||||||
|
// 关联自动写入
|
||||||
|
protected $relationWrite;
|
||||||
//
|
//
|
||||||
protected static $db;
|
protected static $db;
|
||||||
|
|
||||||
@@ -657,6 +659,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联数据一起更新
|
||||||
|
* @access public
|
||||||
|
* @param mixed $relation 关联
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function together($relation)
|
||||||
|
{
|
||||||
|
if (is_string($relation)) {
|
||||||
|
$relation = explode(',', $relation);
|
||||||
|
}
|
||||||
|
$this->relationWrite = $relation;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取模型对象的主键
|
* 获取模型对象的主键
|
||||||
* @access public
|
* @access public
|
||||||
@@ -715,6 +732,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 自动关联写入
|
||||||
|
if (!empty($this->relationWrite)) {
|
||||||
|
$relation = [];
|
||||||
|
foreach ($this->relationWrite as $name) {
|
||||||
|
if (isset($this->data[$name])) {
|
||||||
|
$relation[$name] = $this->data[$name];
|
||||||
|
if (!$this->isUpdate) {
|
||||||
|
unset($this->data[$name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 检测字段
|
// 检测字段
|
||||||
if (!empty($this->field)) {
|
if (!empty($this->field)) {
|
||||||
foreach ($this->data as $key => $val) {
|
foreach ($this->data as $key => $val) {
|
||||||
@@ -775,7 +805,29 @@ 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);
|
||||||
|
|
||||||
|
// 关联更新
|
||||||
|
if (isset($relation)) {
|
||||||
|
foreach ($relation as $name => $val) {
|
||||||
|
if ($val instanceof Model) {
|
||||||
|
$val->save();
|
||||||
|
} else {
|
||||||
|
$this->getAttr($name)->save($val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 清空change
|
// 清空change
|
||||||
$this->change = [];
|
$this->change = [];
|
||||||
// 更新回调
|
// 更新回调
|
||||||
@@ -802,6 +854,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$this->data[$pk] = $insertId;
|
$this->data[$pk] = $insertId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 关联写入
|
||||||
|
if (isset($relation)) {
|
||||||
|
foreach ($relation as $name => $val) {
|
||||||
|
$method = Loader::parseName($name, 1, false);
|
||||||
|
$this->$method()->save($val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 标记为更新
|
// 标记为更新
|
||||||
$this->isUpdate = true;
|
$this->isUpdate = true;
|
||||||
// 清空change
|
// 清空change
|
||||||
|
|||||||
@@ -276,7 +276,7 @@ abstract class OneToOne extends Relation
|
|||||||
// 组装模型数据
|
// 组装模型数据
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($list as $set) {
|
foreach ($list as $set) {
|
||||||
$data[$set->$key][] = $set;
|
$data[$set->$key] = $set;
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user