# 模型
StartCMS基础模型继承了TP基础模型,所以拥有TP模型的原本特性,同时拓展新增了以下属性和内置方法
- 自动化属性
- 数据授权属性:$auth = [];
- 自动关联属性:$with = [];
- 自动排序属性:$order = [];
- 自动过滤属性:$where = [];
- 综合搜索方法:filter()
- 常用查询方法:
- 详情查询:info($filter = [], $with = null)
- 列表查询:list($filter = [], $order = [], $with = null)
- 分页查询:page($filter = [], $order = [], $with = null, $paging = [])
- 软删除:remove()
提示
- 内置CURD查询方法中 $filter 参数都支持综合查询方法filter()所接受的参数格式,具体格式请查看综合查询filter
- 内置CURD查询方法中 $with 参数为null时会根据自动关联属性进行关联查询,传入具体关联内后不再自动关联
# 模型定义
模型类的命名规则是除去表前缀的数据表名称,采用驼峰法命名并且首字母大写,模型会自动对应数据表
只有继承于start\Model才能使用框架拓展的属性和方法
如果你的规则和上面的系统约定不符合,那么需要设置Model类的数据表名称属性,以确保能够找到对应的数据表。
模型自动对应的数据表名称都是遵循小写+下划线规范,如果你的表名有大写的情况,必须通过设置模型的table属性。
提示
建议数据表命名统一使用应用名为前缀,如:
cms应用: cms_article, cms_category, cms_comment
client应用:client_profile, client_company, client_address
一个最简模型类:
<?php
namespace app\cms\model;
use start\Model;
class Article extends Model
{
protected $name = 'cms_article';
}
一个包含自动属性的模型类:
<?php
namespace app\cms\model;
use start\Model;
class Article extends Model
{
// 数据表名
protected $name = 'cms_article';
// 数据授权(仅查询owner_id = get_user_id()的数据记录)
protected $auth = ['owner_id'];
// 自动关联(通过list、page、info方法查询时会自动关联预载入)
protected $with = ['category','comment'];
// 自动排序(通过list、page、info方法查询时会自动排序)
protected $order = ['create_time desc'];
// 自动过滤(通过list、page,、info方法查询时会添加查询where条件)
protected $where = ['status' => 1];
// 关联分类并绑定分类名
public function category()
{
return $this
->belongsTo(Category::class, 'category_id', 'id')
->bind(['category_title' => 'title']);
}
// 关联评论
public function comment()
{
return $this
->hasMany(Comment::class, 'article_id', 'id');
}
}
# 综合查询
filter
方法是StartCMS定义的关键查询方法,模型内置常用方法都支持该查询,可以一次性完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询,关联查询在内的查询操作。
提示
filrer
方法的参数支持的变量类型包括字符串、数组。 当filter
方法接收的参数为字符串时,直接调用where查询
# 参数支持
filter
方法参数为数组时,支持多个key=>value
条件同时查询,数组项之间为AND
链接
key
支持使用|
进行或查询,支持使用.
进行关联查询,也可以使用@
进行表达式查询,如:
// 或查询:查询 key1=value 或 key2=value 的记录
->filter(['key1|key2' => 'value']);
// 关联查询:查询关联模型model中 key=value 的记录
->filter(['model.key' => 'value']);
// 表达式查询:查询关联模型model中 key>value 的记录
->filter(['model.key@>' => 'value']);
当
value
为数组而key
中没有使用@定义操作符时,默认为in查询
表达式为小写,支持的查询表达式有下面几种:
表达式 | 含义 | 使用方法 |
---|---|---|
= | 等于 | $model->filter(['key' => 'value']) |
<> | 不等于 | $model->filter(['key@<>' => 'value']) |
> | 大于 | $model->filter(['key@>' => 'value']) |
>= | 大于等于 | $model->filter(['key@>=' => 'value']) |
< | 小于 | $model->filter(['key@<' => 'value']) |
<= | 小于等于 | $model->filter(['key@<=' => 'value']) |
[not] like | 模糊查询 | $model->filter(['key@like' => 'value']) $model->filter(['key@not like' => 'value']) |
[not] between | (不在)区间查询 | $model->filter(['key@between' => array]) $model->filter(['key@not between' => array]) |
[not] in | (不在)IN 查询 | $model->filter(['key@in' => array]) $model->filter(['key@not in' => array]) |
[not] null | 查询字段是否(不)是NULL | $model->filter(['key@null' => value]) $model->filter(['key@not null' => value]) |
[not] exists | EXISTS查询 | $model->filter(['key@exists' => value]) $model->filter(['key@not exists' => value]) |
[not] regexp | 正则(不)匹配查询(仅支持Mysql) | $model->filter(['key@regexp' => value]) $model->filter(['key@not regexp' => value]) |
# 所支持的查询条件示例
$model = new Article
$input = [
'key1' => 'value', // 等值查询 等同于 $model->where(key1, '=', 'value')
'key2@like' => 'value', // 模糊查询 等同于 $model->where(key2, 'like', "%value%")
'key1|key2' => 'value', // OR查询 等同于 $model->whereOr(key1, '=', 1)->whereOr(key2, '=', 1)
'key2' => [1,2], // IN查询 等同于 $model->where(key2, 'in', [1,2])
'key2@between' => [1,3], // 范围查询 等同于 $model->where(key3, 'between', [1,3])
'key1|key2@like' => 'value', // 综合查询 等同于 $model->whereOr(key1, '=', 'value')->whereOr(key2, 'like', '%value%')
// 关联查询:(以下model为模型关联方法名)
'model.key1' => 'value', // 关联等值查询 等同于 $model->join('model_table', 'model_table.key1' = 'value')
'model.key2@like' => 'value' // 关联模糊查询 等同于 $model->join('model_table', 'model_table.key2 like %value%')
'model.key1|key2' => 'value' // 关联或查询
'model1.key1|model2.key2' => 'value', // 多关联或查询
];
$data = $model->filter($input)->select();
# 详情查询
模型查询和数据库查询方法的区别主要在于,模型中的查询的数据在获取的时候会经过获取器的处理,以及更加对象化的获取方式。StartCMS基础模型以内置记录查询方法会自动关联查询
/**
* 获取详情
* @param array $filter 综合查询条件
* @param array $with 指定关联模型(不指定时根据属性自动关联)
* @return object 对象
*/
public function info($filter, $with = null)
{
...
}
// 模型调用方式
$model = new Atricle();
$data = $model->info($id);
// 服务调用方式
$data = ArticleService::getInfo($id);
# 列表查询
获取所有数据,实现业务接口请思考是否需要对该接口进行数量限制或权限限制,以免数据过量或暴露
StartCMS基础模型以内置列表记录查询方法会自动关联查询
/**
* 获取所有数据
* @param array $filter 综合查询条件
* @param array $order 指定排序方式(不指定时根据属性自动排序)
* @param array $with 指定关联模型(不指定时根据属性自动关联)
* @return \think\model\Collection 数据集
*/
function list($filter = [], $order = [], $with = null, $limit = null)
{
...
}
...
// 模型调用方式
$input = [];
$model = new Atricle();
$data = $model->list($input);
// 服务调用方式
$data = ArticleService::getList($input);
# 分页查询
获取分页数据,
StartCMS基础模型内置分页查询方法会自动关联查询
/**
* 获取所有数据
* @param array $filter 综合查询条件
* @param array $order 指定排序方式(不指定时根据属性自动排序)
* @param array $with 指定关联模型(不指定时根据属性自动关联)
* @param array $paging 指定分页方式(不指定时根据接口参数page,per_page进行分页)
* @return \think\model\Collection 数据集
*/
function page($filter = [], $order = [], $with = null, $paging = [])
{
...
}
...
// 模型调用方式
$input = [];
$model = new Atricle();
$data = $model->page($input);
// 服务调用方式
$data = ArticleService::getPage($input);