mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-02 21:52:34 +08:00
Model类增加一对一关联自动删除功能
This commit is contained in:
@@ -831,7 +831,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
if ($val instanceof Model) {
|
||||
$val->save();
|
||||
} else {
|
||||
$this->getAttr($name)->save($val);
|
||||
$model = $this->getAttr($name);
|
||||
if ($model instanceof Model) {
|
||||
$model->save($val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -987,8 +990,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
return false;
|
||||
}
|
||||
|
||||
// 删除当前模型数据
|
||||
$result = $this->db()->delete($this->data);
|
||||
|
||||
// 关联删除
|
||||
if (!empty($this->relationWrite)) {
|
||||
foreach ($this->relationWrite as $key => $name) {
|
||||
$name = is_numeric($key) ? $name : $key;
|
||||
$model = $this->getAttr($name);
|
||||
if ($model instanceof Model) {
|
||||
$model->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->trigger('after_delete', $this);
|
||||
return $result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user