框架中提供了一个模型基类:app/BaseModel
BaseModel.php结构如下:
declare(strict_types=1);
namespace app;
use think\db\BaseQuery;
use think\db\exception\DbException;
use think\facade\Db;
use think\Model;
class BaseModel extends Model
{
/**
* 默认主键
* @var string
*/
protected $pk = 'id';
/**
* 数据表
* @var string
*/
//protected static $table = '';
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 = '';
/**
* 是否分区
* @var array
*/
protected $isPartition = false;
protected $cachePrefix;
public function __construct($data = [])
{
parent::__construct($data);
$this->cachePrefix = getenv('DATABASE_HOSTNAME') . getenv('DATABASE_DATABASE');
}
public function getTablePrefix(){
return config('database.connections')[config('database.default')]['prefix'];
}
/**
* 查询缓存 数据为空不缓存
* @access public
* @param mixed $key 缓存key
* @param integer|\DateTime $expire 缓存有效期
* @param null $tag
* @param bool $always
* @return mixed
*/
public function cache($key = true, $expire = null, $tag = null, bool $always = false){
$key = $this->getCacheKey($key);
return parent::cache($key, $expire, $tag, $always);
}
/**
* 缓存key策略
* @param $key
* @return string
* Author: fudaoji<fdj@kuryun.cn>
*/
public function getCacheKey($key){
return $this->isCache ? md5(self::getTable() . serialize($key)) : $this->isCache;
}
/**
* 更新数据
* @access public
* @param array $data 数据数组
* @param mixed $where 更新条件
* @param array $allowField 允许字段
* @param string $suffix 数据表后缀
* @return Model
* @throws DbException
*/
public static function update(array $data, $where = [], array $allowField = [], string $suffix = '')
{
unset($data['__token__']);
return parent::update($data, $where, $allowField, $suffix);
}
/**
* 模型新增数据
* @param array $data
* @param array $allowField
* @param bool $replace
* @param string $suffix
* @return Model
* Author: fudaoji<fdj@kuryun.cn>
*/
public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
{
unset($data['__token__']);
return parent::create($data, $allowField, $replace, $suffix); // TODO: Change the autogenerated stub
}
/**
* 模型新增数据
* @param array $data
* @param array $allowField
* @param bool $replace
* @param string $suffix
* Author: fudaoji<fdj@kuryun.cn>
* @return array
*/
public function createOne(array $data)
{
unset($data['__token__']);
if($this->autoWriteTimestamp){
$this->createTime && empty($data[$this->createTime]) && $data[$this->createTime] = time();
$this->updateTime && empty($data[$this->updateTime]) && $data[$this->updateTime] = time();
}
$data[$this->pk] = $this->insertGetId($data);
return $data;
}
/**
* 数据库的更新,保存当前数据对象
* @access public
* @param array $data 数据
* @param string $sequence 自增序列名
* @return bool
*/
public function save(array $data = [], string $sequence = null): bool
{
unset($data['__token__']);
return parent::save($data, $sequence);
}
}我们写普通模型类时,尽量继承这个BaseModel.php。