Skip to content

Commit

Permalink
修正软删除 模型增加isForce和isExists方法
Browse files Browse the repository at this point in the history
软删除的restore方法返回值改为布尔值
软删除的delete方法force参数取消 改为使用force方法
  • Loading branch information
liu21st committed Jun 8, 2018
1 parent 771daea commit e0a8bdc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
23 changes: 22 additions & 1 deletion library/think/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,16 @@ public function force($force = true)
return $this;
}

/**
* 判断force
* @access public
* @return bool
*/
public function isForce()
{
return $this->force;
}

/**
* 新增数据是否使用Replace
* @access public
Expand All @@ -367,7 +377,18 @@ public function replace($replace = true)
}

/**
* 新增数据是否使用Replace
* 设置数据是否存在
* @access public
* @param bool $exists
* @return void
*/
public function exists($exists)
{
$this->exists = $exists;
}

/**
* 判断数据是否存在数据库
* @access public
* @return bool
*/
Expand Down
31 changes: 15 additions & 16 deletions library/think/model/concern/SoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,17 @@ protected function getWithTrashedExp()
/**
* 删除当前的记录
* @access public
* @param bool $force 是否强制删除
* @return bool
*/
public function delete($force = false)
public function delete()
{
if (!$this->exists || false === $this->trigger('before_delete', $this)) {
if (!$this->isExists() || false === $this->trigger('before_delete', $this)) {
return false;
}

$name = $this->getDeleteTimeField();

if ($name && !$force) {
if ($name && !$this->isForce()) {
// 软删除
$this->data($name, $this->autoWriteTimestamp($name));

Expand All @@ -104,7 +103,7 @@ public function delete($force = false)

$this->trigger('after_delete', $this);

$this->exists = false;
$this->exists(false);

return true;
}
Expand Down Expand Up @@ -135,7 +134,7 @@ public static function destroy($data, $force = false)

if ($resultSet) {
foreach ($resultSet as $data) {
$data->delete($force);
$data->force($force)->delete();
}
}

Expand All @@ -146,35 +145,35 @@ public static function destroy($data, $force = false)
* 恢复被软删除的记录
* @access public
* @param array $where 更新条件
* @return integer
* @return bool
*/
public function restore($where = [])
{
$name = $this->getDeleteTimeField();

if (empty($where)) {
$pk = $this->getPk();

$where[] = [$pk, '=', $this->getData($pk)];
}

if ($name) {
if (false === $this->trigger('before_restore')) {
return false;
}

if (empty($where)) {
$pk = $this->getPk();

$where[] = [$pk, '=', $this->getData($pk)];
}

// 恢复删除
$result = $this->db(false)
$this->db(false)
->where($where)
->useSoftDelete($name, $this->getWithTrashedExp())
->update([$name => $this->defaultSoftDelete]);

$this->trigger('after_restore');

return $result;
return true;
}

return 0;
return false;
}

/**
Expand Down

0 comments on commit e0a8bdc

Please sign in to comment.