框架中有两个模型基类:extend/ky/BaseModel 和 application/common/model/Base
BaseModel.php结构如下:
namespace ky; use think\Db; use think\db\Where; use think\Model; class BaseModel extends Model { /** * 默认主键 * @var string */ protected $pk = 'id'; //是否开启默认写入时间字段 protected $autoWriteTimestamp = true; // 创建时间字段,无填null protected $createTime = 'create_time'; // 更新时间字段,无填null protected $updateTime = 'update_time'; /** * 是否开启缓存 * @var bool */ protected $isCache = false; /** * 缓存tag * @var string */ protected $cacheTag = ''; /** * 缓存时间 * @var int */ protected $expire = 3600; /** * 水平分表规则 * @var array */ protected $rule = []; /** * 分表字段 * @var string */ protected $key = ''; public function __construct($data = []) { parent::__construct($data); } /** * 根据条件和排序获取单条数据 * @param array $params * @return array|false|\PDOStatement|string|Model * @throws \think\exception\DbException * @Author: fudaoji <fdj@kuryun.cn> */ public function getOneByOrder($params = []){} /** * 根据条件删除数据 * @param array $map * @return int * @throws \think\Exception * @throws \think\exception\PDOException * @throws \Exception */ public function delByMap($map = []){} /** * 设置临时缓存状态 * @param bool $v * @return $this * @author: fudaoji<fdj@kuryun.cn> */ public function setCache($v = false){} /** * 查询某个时间段的count统计 * @param array $params * @return mixed * @Author Doogie<461960962@qq.com> * @throws \Exception */ public function totalByTime($params = []){} /** * 根据主键获取单个数据 * @param int $pk * @param int $refresh * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @Author Doogie<461960962@qq.com> */ public function getOne($pk = 0, $refresh = 0){} /** * 新增单条数据 * @param array $data * @return bool|mixed * @throws \think\Exception * @Author Doogie<461960962@qq.com> */ public function addOne($data = []){} /** * 批量添加 * @param array $arr * @return bool * @Author: fudaoji<fdj@kuryun.cn> * @throws \think\Exception */ public function addBatch($arr = []){} /** * 更新单条数据 * @param array $data * @return bool|mixed * @throws \think\Exception * @throws \think\exception\PDOException * @Author fudaoji<fdj@kuryun.cn> */ public function updateOne($data = []){} /** * 根据条件更新数据 * @param array $where * @param array $data * @return bool|mixed * @throws \think\Exception * @throws \think\exception\PDOException * @author: fudaoji<fdj@kuryun.cn> */ public function updateByMap($where = [], $data = []){} /** * 批量修改数据 * @param $arr * @return bool * @throws \think\Exception * @throws \think\exception\PDOException */ public function updateBatch($arr){} /** * 根据主键删除单个数据 * @param int $pk * @return bool * @throws \think\Exception * @throws \think\exception\PDOException */ public function delOne($pk=0){} /** * 批量删除 * @param array $pk_arr * @return bool * @throws \think\Exception * @throws \think\exception\PDOException */ public function delBatch($pk_arr=[]){} /** * 自带的分页 * @param $where * @param $order * @param $page_size * @param mixed $field * @param $refresh * @return mixed * @auth Doogie<461960962@qq.com> * @throws \think\exception\DbException */ public function page($page_size=10, $where=[], $order=[], $field = true, $refresh=0){} /** * 获取分页数据 * @param array $limit * @param array $where * @param array $order * @param bool|true $field * @param int $refresh * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @Author Doogie<461960962@qq.com> */ public function getList($limit = [], $where = [], $order = [], $field = true, $refresh = 0){} /** * count统计 * @param array $where * @param int $refresh * @return mixed * @Author Doogie<461960962@qq.com> */ public function total($where = [], $refresh = 0){} /** * 获取某些字段 * @param string $field * @param array $query * @param int $refresh * @return array * @Author Doogie<461960962@qq.com> */ public function getField($field = '', $query = [],$refresh = 0){} /** * 条件链接器 * @param $self * @param array $where * @Author: Doogie <461960962@qq.com> * @return BaseModel */ private function _where(&$self, $where=[]){} /** * 根据条件获取单条数据 * @param array $where * @param $field * @param int $refresh * @param array $order 排序获取 * @return array|false|\PDOStatement|string|Model * @throws \think\exception\DbException * @Author: fudaoji <fdj@kuryun.cn> */ public function getOneByMap($where=[], $field = true, $refresh = 0, $order = []){} /** * 获取builder,兼容分表 * @param array $query * @return $this|\think\db\Query * @Author: Doogie <461960962@qq.com> */ public function getBuilder($query = []){} /** * 获取真正的表名 * @param array $query * @return mixed * @Author: Doogie <461960962@qq.com> */ public function getTrueTable($query = []){} } /** * 获取真实id值 * @param array $query * @return array * @Author: Doogie <461960962@qq.com> */ public function getId($query = []){} /** * 根据条件获取数据 * @param array $params * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @author: Doogie<461960962@qq.com> */ public function getAll($params){} /** *根据某个字段不重复获取数据 * @param string $field 字段 * @param array $where * @param int $refresh * @return mixed * @Author Doogie<461960962@qq.com> */ public function distinctField($field = '', $where = [], $refresh = 0){} /** * 获取group by结果列表 * @param array $params 参数 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @author: Doogie<461960962@qq.com> */ public function getGroupList($params = []){} /** * sum求和 * @param string $field * @param array $where * @param int $refresh * @return int $data * @author Jason<1589856452@qq.com> */ public function sums($field = '', $where = [], $refresh = 0) {} /** * 获取有联合查询的分页数据 * @param array $params * @return mixed * e.g: model('activity')->getListJoin([ * 'alias' => 'a', * 'join' => [[config('database.prefix').'user u', 'a.user_id=u.id', 'left']], * 'limit' => [1, 100], * 'where' => ['a.id' => ['gt', 300]], * 'field' => 'u.username,a.id as activity_id', * 'order' => ['a.id' => 'desc'] * ]); * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @Author Doogie<461960962@qq.com> */ public function getListJoin($params = []){} /** * 获取关联查询所有数据 * @param array $params * @return mixed * e.g: model('activity')->getAllJoin([ * 'alias' => 'a', * 'join' => [[config('database.prefix').'user u', 'a.user_id=u.id', 'left']], * 'where' => ['a.id' => ['gt', 300]], * 'field' => 'u.username,a.id as activity_id', * 'order' => ['a.id' => 'desc'] * ]); * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @author Jason<dcq@kuryun.cn> */ public function getAllJoin($params = []){} /** * 获取多表关联统计数据 * @param array $params * @return mixed * e.g: model('activity')->totalJoin([ 'alias' => 'a', 'join' => [[config('database.prefix').'user u', 'a.user_id=u.id', 'left']], 'where' => ['a.id' => ['gt', 300]] ]); * @author Jason<dcq@kuryun.cn> */ public function totalJoin($params = []){} /** * 获取有联合查询的分页数据 * @param array $params * @return mixed * e.g: model('activity')->pageJoin([ * 'alias' => 'a', * 'join' => [[config('database.prefix').'user u', 'a.user_id=u.id', 'left']], * 'page_size' => 20, * 'where' => ['a.id' => ['gt', 300]], * 'field' => 'u.username,a.id as activity_id', * 'order' => ['a.id' => 'desc'] * ]); * @Author Doogie<461960962@qq.com> * @throws \think\exception\DbException */ public function pageJoin($params = []){} /** * 获取group by结果 * @param array $params 参数 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @author: Doogie<461960962@qq.com> */ public function getGroupAll($params = []){} /** * 获取单条数据的关联查询 * @param array $params * @return mixed * e.g: model('user')->getOneJoin([ * 'alias' => 'u', * 'join' => [['profile p', 'p.user_id=u.id']], * 'where' => ['u.id' => 1] * ]); * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @author fudaoji<fdj@kuryun.cn> */ public function getOneJoin($params = []){} /** * 获取分组统计数据 * @param array $params * @return mixed * e.g: model('activity')->totalGroup([ 'where' => ['a.id' => ['gt', 300]], 'group' => 'activity.id', 'having' => 'activity.id > 100', 'refresh' => 1 ]); * @author fudaoji<fdj@kuryun.cn> */ public function totalGroup($params = []){} }
Base.php结构如下:
namespace app\common\model; use ky\BaseModel; class Base extends BaseModel { /** * 手动设置分表key,因为多表关联查询时会有表别名,因此需要临时修改分表key * Author: fudaoji<fdj@kuryun.cn> * @param string $key * @return \ky\BaseModel */ public function setKey($key = ''){} /** * 获取实际表名,用于被关联表有多个分表的情况 * @param $key_id * @return string * Author: fudaoji<fdj@kuryun.cn> */ public function getRealTableName($key_id){} }
我们写普通模型类时,尽量继承这个Base.php。