改进Model类的delete方法 支持没有主键情况下的删除操作

This commit is contained in:
thinkphp
2017-01-20 21:26:51 +08:00
parent 249c5e62e4
commit 48b856a7f1

View File

@@ -991,8 +991,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
// 删除条件
$pk = $this->getPk();
if (isset($this->data[$pk])) {
$where = [$pk => $this->data[$pk]];
} elseif (!empty($this->updateWhere)) {
$where = $this->updateWhere;
} else {
$where = null;
}
// 删除当前模型数据
$result = $this->db()->delete($this->data);
$result = $this->db()->where($where)->delete();
// 关联删除
if (!empty($this->relationWrite)) {