95 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			95 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace app\common\model\school;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use app\common\model\BaseModel;
							 | 
						||
| 
								 | 
							
								use think\Model;
							 | 
						||
| 
								 | 
							
								use traits\model\SoftDelete;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class SportApply extends BaseModel
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    use SoftDelete;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 表名
							 | 
						||
| 
								 | 
							
								    protected $name = 'school_sport_apply';
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    // 自动写入时间戳字段
							 | 
						||
| 
								 | 
							
								    protected $autoWriteTimestamp = 'integer';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 定义时间戳字段名
							 | 
						||
| 
								 | 
							
								    protected $createTime = 'createtime';
							 | 
						||
| 
								 | 
							
								    protected $updateTime = 'updatetime';
							 | 
						||
| 
								 | 
							
								    protected $deleteTime = 'deletetime';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 追加属性
							 | 
						||
| 
								 | 
							
								    protected $append = [
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    ];
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function user()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /** 通用新增(后台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{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            //是否采用模型验证
							 | 
						||
| 
								 | 
							
								            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;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 |