恢复数据:restore( )
- 功能:恢复被用户软删除的数据。
我们为什么要用软删除,目的之一就是以后可以根据需要进行恢复。
- 源码: /thinkphp/library/traits/model/SoftDelete.php
3.模型中定义方法
/**
* 恢复被软删除的记录
* @access public
* @param array $where 更新条件
* 参数为空,则恢复全部被软删除的数据
* @return integer
*/
public function restore($where = [])
{
$name = $this->getDeleteTimeField();
// 恢复删除
return $this->isUpdate()->save([$name => null], $where);
}
看过源码发现,所谓恢复就是将delete_time字段置null值,与下面SQL语句等价:
UPDATE tp5_staff SET delete_time = NULL ;
- 实例:
**任务:恢复id=35的记录
<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
public function index()
{
$goodsModel = new GoodsModel();
//数据恢复
$res = $goodsModel->restore(['id' => 35]);
var_dump($res);die;
}
}