mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 15:42:48 +08:00
Model类增加一对一关联自动删除功能
This commit is contained in:
@@ -831,7 +831,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
if ($val instanceof Model) {
|
if ($val instanceof Model) {
|
||||||
$val->save();
|
$val->save();
|
||||||
} else {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 删除当前模型数据
|
||||||
$result = $this->db()->delete($this->data);
|
$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);
|
$this->trigger('after_delete', $this);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user