mirror of https://gitee.com/karson/fastadmin.git
增加模型基类,便于扩展和重写方法。
parent
dca32b0a13
commit
2433a367ab
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
/**
|
||||
* 基类模型
|
||||
*
|
||||
*/
|
||||
class Base extends Model
|
||||
{
|
||||
public function initialize() {
|
||||
parent::initialize();
|
||||
// ...
|
||||
}
|
||||
|
||||
// 通用方法或重写方法
|
||||
/**
|
||||
* 解析信息详情传入参数
|
||||
* @param unknown $params
|
||||
* @return \app\common\model\Base
|
||||
*/
|
||||
public function infoConditions($params=null) {
|
||||
if( isset($params['where']) && $params['where'] ) {
|
||||
$this->where($params['where']);
|
||||
}
|
||||
if( isset($params['extra_where']) && $params['extra_where']) {
|
||||
$this->where($params['extra_where']);
|
||||
}
|
||||
|
||||
if( isset($params['field']) && $params['field'] ) {
|
||||
$this->field($params['field']);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* 解析列表传入参数
|
||||
* @param string $params
|
||||
* @return \app\common\model\Base|number
|
||||
*/
|
||||
public function listConditions($params)
|
||||
{
|
||||
if( isset($params['where']) && $params['where'] ) {
|
||||
$this->where($params['where']);
|
||||
}
|
||||
if( isset($params['extra_where']) && $params['extra_where']) {
|
||||
$this->where($params['extra_where']);
|
||||
}
|
||||
$total = $this->count();
|
||||
if( isset($params['get_count']) && $params['get_count'] ) {
|
||||
return $total;
|
||||
}
|
||||
if( isset($params['where']) && $params['where'] ) {
|
||||
$this->where($params['where']);
|
||||
}
|
||||
if( isset($params['extra_where']) && $params['extra_where']) {
|
||||
$this->where($params['extra_where']);
|
||||
}
|
||||
if( isset($params['field']) && $params['field'] ) {
|
||||
$this->field($params['field']);
|
||||
}
|
||||
if( isset($params['sort']) && $params['sort'] && isset($params['sort']) && $params['order'] ) {
|
||||
$this->order($params['sort'], $params['order']);
|
||||
}
|
||||
if( isset($params['offset']) && $params['offset'] < 0 ) {
|
||||
$params['offset'] = 0;
|
||||
}
|
||||
if( isset($params['limit']) && $params['limit'] > 0 ) {
|
||||
$this->limit($params['offset'], $params['limit']);
|
||||
} else {
|
||||
if( isset($params['group']) && $params['group'] ) {
|
||||
$this->group($params['group']);
|
||||
}
|
||||
}
|
||||
return ['total'=>$total,'model'=>$this];
|
||||
}
|
||||
/**
|
||||
* 格式化列表
|
||||
* @param unknown $list
|
||||
* @param unknown $format ['key_value'=>'id','key_list'=>false] 当key_value存在(必须为唯一值,例id),key_list为真,列表键值按此字段 ,否则返回原列表
|
||||
* @return array|unknown|unknown[]|unknown
|
||||
*/
|
||||
public function formatList($list,$format) {
|
||||
if(!$list) {
|
||||
return [];
|
||||
}
|
||||
$newList = [];
|
||||
if( isset($format['key_value']) && $format['key_value'] ) {
|
||||
foreach ($list as $val) {
|
||||
$newList[$val[$format['key_value']]] = $val;
|
||||
}
|
||||
unset($val);
|
||||
}
|
||||
if( isset($format['key_list']) && $format['key_list'] ) {
|
||||
return $newList ?? $list;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue