关联自动删除增加一对多关联删除支持

This commit is contained in:
thinkphp
2017-11-24 15:26:09 +08:00
parent a1abcebbd8
commit b69e64a521

View File

@@ -53,7 +53,9 @@ trait SoftDelete
*/
public function delete($force = false)
{
if (false === $this->trigger('before_delete', $this)) return false;
if (false === $this->trigger('before_delete', $this)) {
return false;
}
$name = $this->getDeleteTimeField();
if (!$force) {
@@ -69,9 +71,14 @@ trait SoftDelete
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();
$result = $this->getRelation($name);
if ($result instanceof Model) {
$result->delete();
} elseif ($result instanceof Collection || is_array($result)) {
foreach ($result as $model) {
$model->delete();
}
}
}
}
@@ -92,7 +99,9 @@ trait SoftDelete
*/
public static function destroy($data, $force = false)
{
if (is_null($data)) return 0;
if (is_null($data)) {
return 0;
}
// 包含软删除数据
$query = self::withTrashed();
@@ -160,7 +169,9 @@ trait SoftDelete
$this->deleteTime :
'delete_time';
if (!strpos($field, '.')) $field = '__TABLE__.' . $field;
if (!strpos($field, '.')) {
$field = '__TABLE__.' . $field;
}
if (!$read && strpos($field, '.')) {
$array = explode('.', $field);