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', '')); } $rule = [ 'user_id'=>'require', 'name'=>'require', 'idnum'=>'require', ]; $rule_msg = [ "user_id.require"=>'用户必填', "name.require"=>'姓名必填', "idnum.require"=>'身份证号必填', ]; self::check($params,$rule,$rule_msg); //判断逻辑 if($trans){ self::beginTrans(); } $res = true; try{ //是否采用模型验证 $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; } /** 通用编辑(后台api版本) * @param $params * @param $trans * @return $this * @throws \Exception */ public function edit($id,$params,$trans=false){ $row = $this->get($id); if (!$row) { throw new \Exception(__('No Results were found')); } if (empty($params)) { throw new \Exception(__('Parameter %s can not be empty', '')); } $rule = [ 'user_id'=>'require', 'name'=>'require', 'idnum'=>'require', ]; $rule_msg = [ "user_id.require"=>'用户必填', "name.require"=>'姓名必填', "idnum.require"=>'身份证号必填', ]; self::check($params,$rule,$rule_msg); //判断逻辑 if($trans){ self::beginTrans(); } $res = true; try{ $result = $row->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 $row; } /** 通用详情(后台api版本) * @param $params * @param $trans * @return $this * @throws \Exception */ public function detail($id,$show_field=[],$except_field=[]){ $row = $this->get($id); if (!$row) { throw new \Exception(__('No Results were found')); } if($show_field){ $row->visible($show_field); } if($except_field){ $row->hidden($except_field); } return $row; } // public function index($page,$limit,$where=[]) // { // $adminIds = $this->getDataLimitAdminIds(); // $aliasName = "" ; // if (is_array($adminIds)) { // $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds]; // } // // } /** 通用删除(后台api版本) * @param $params * @param $trans * @return $this * @throws \Exception */ public function del($ids = null,$trans=false){ if (empty($ids)) { throw new \Exception(__('Parameter %s can not be empty', 'ids')); } //判断逻辑 $pk = $this->getPk(); $list = $this->where($pk, 'in', $ids)->select(); $count = 0; if($trans){ self::beginTrans(); } $res = true; try{ foreach ($list as $item) { $count += $item->delete(); } if($trans){ self::commitTrans(); } }catch (\Exception $e){ if($trans){ self::rollbackTrans(); } throw new \Exception($e->getMessage().$e->getFile().$e->getLine()); } return $count; } /**得到基础条件 * @param $status * @param null $model * @param string $alisa */ public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false) { if (!$model) { $model = new static; if ($alisa&&!$with) $model = $model->alias($alisa); } if ($alisa) $alisa = $alisa . '.'; $tableFields = (new static)->getTableFields(); foreach ($tableFields as $fields) { if(in_array($fields, ["user_id"]))continue; // if (isset($whereData[$fields]) && $whereData[$fields]) $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]); if (isset($whereData[$fields]) && $whereData[$fields]){ if(is_array($whereData[$fields])){ $model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]); }else{ $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]); } } } if (isset($whereData['user_id']) && $whereData['user_id']) $model = $model->where("{$alisa}user_id", 'in', $whereData['user_id']); if (isset($whereData['keywords'])&&$whereData['keywords']) $model = $model->where("{$alisa}name|{$alisa}idnum", 'like', "%". $whereData['keywords']."%"); if (isset($whereData['time'])&&$whereData['time']){ $model = $model->time(["{$alisa}createtime",$whereData['time']]); } return $model; } public static function joinList($page, $limit,$params=[]){ $with_field = [ 'base'=>['*'], ]; // $alisa = (new self)->getWithAlisaName(); // $sort = "{$alisa}.id desc"; $sort = "id desc"; $serch_where = []; $serch_where = array_merge($serch_where,$params); return (new self)->getBaseList($serch_where, $page, $limit,$sort,$with_field); } }