97 lines
2.1 KiB
PHP
Raw Normal View History

2025-05-20 16:33:23 +08:00
<?php
namespace app\common\model\home;
use app\common\model\BaseModel;
use think\Model;
class LeaveWord extends BaseModel
{
// 表名
protected $name = 'leave_word';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'status_text'
];
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ?: ($data['status'] ?? '');
$list = $this->getStatusList();
return $list[$value] ?? '';
}
/** 通用新增(后台api版本)
* @param $params
* @param $trans
* @return $this
* @throws \Exception
*/
public function add($params,$trans=false){
if (empty($params)) {
throw new \Exception(__('Parameter %s can not be empty', ''));
}
// if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
// $params[$this->dataLimitField] = $this->auth->id;
// }
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
$params["status"] = '1';
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->validateFailException()->validate($validate);
}
$result = $this->allowField(true)->save($params);
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
}
return $this;
}
}