2019-11-28 11:24:34 +08:00

86 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | Library for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://library.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/zoujingli/ThinkLibrary
// | github 仓库地址 https://github.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------
namespace library\helper;
use library\Helper;
use think\db\Query;
/**
* 通用删除管理器
* Class DeleteHelper
* @package library\helper
*/
class DeleteHelper extends Helper
{
/**
* 表单额外更新条件
* @var array
*/
protected $where;
/**
* 数据对象主键名称
* @var string
*/
protected $field;
/**
* 数据对象主键值
* @var string
*/
protected $value;
/**
* 逻辑器初始化
* @param string|Query $dbQuery
* @param string $field 操作数据主键
* @param array $where 额外更新条件
* @return boolean|null
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function init($dbQuery, $field = '', $where = [])
{
$this->where = $where;
$this->query = $this->buildQuery($dbQuery);
$this->field = empty($field) ? $this->query->getPk() : $field;
$this->value = $this->app->request->post($this->field, null);
// 主键限制处理
if (!isset($this->where[$this->field]) && is_string($this->value)) {
$this->query->whereIn($this->field, explode(',', $this->value));
}
// 前置回调处理
if (false === $this->controller->callback('_delete_filter', $this->query, $where)) {
return null;
}
// 执行删除操作
if (method_exists($this->query, 'getTableFields') && in_array('is_deleted', $this->query->getTableFields())) {
$result = $this->query->where($this->where)->update(['is_deleted' => '1']);
} else {
$result = $this->query->where($this->where)->delete();
}
// 结果回调处理
if (false === $this->controller->callback('_delete_result', $result)) {
return $result;
}
// 回复前端结果
if ($result !== false) {
$this->controller->success('数据删除成功!', '');
} else {
$this->controller->error('数据删除失败, 请稍候再试!');
}
}
}