1:全局分表模型定义
<?php
/**
* AdvPut.php
* 全局模型操作
* Created on 2023/6/13 17:21
* Create by xyz
*/
namespace app\common\model;
use think\facade\Env;
use think\Model;
use think\facade\Db;
class BackCommonModel extends Model
{
//分表新表表名
protected string $tableNew = '';
//分表参照表表名
protected string $tableOld = '';
//分表类型:years:年月,specific_date:年月日,year:年,month:月,day:日,delivery:数字取模分表
protected string $tableType = '';
//分表自增id`在这里插入代码片`
protected int $autoIncrement = 1;
//表前缀
// protected $prefix = '';
protected static function init()
{
}
/**
* Created by
* User: xyz
* Date: 2024/2/23
* Time: 17:26
* Brief: 简介
* docs: 自定义模型分表初始方法
* @param int $delivery_number 分表取模数
* @param int $delivery_multiple 分表取模倍数
*/
public function __comInit(int $delivery_number = 0, int $delivery_multiple = 0)
{
//创建分表初始化验证
$this->prefix = Env::get('database.PREFIX', 'tslu_');
// if (!empty($this->prefix) && !empty($this->tableType)) {
if (!empty($this->tableType)) {
//分表尾缀方式
$suffixAll = [
'year' => date("Y"),
'month' => date("m"),
'day' => date("d"),
'years' => date("Y_m"),
'specific_date' => date("Y_m_d"),
'delivery' => 0,
];
//数字取模分表计算
if ($this->tableType == 'delivery' && $delivery_number > 0 && $delivery_multiple > 0) {
$suffixAll['delivery'] = ceil($delivery_number / $delivery_multiple);
}
//验证分表后缀是否存在
if (isset($suffixAll[$this->tableType]) && !empty($suffixAll[$this->tableType])) {
//参照表表名
$this->tableOld = $this->name;
//分表新表表名
$this->tableNew = $this->name . '_' . $suffixAll[$this->tableType];
//验证分表是否存在
$this->gainTable();
}
}
}
/**
* Created by
* User: xyz
* Date: 2024/2/19
* Time: 9:41
* Brief: 简介
* docs: 验证分表是否存在,不存在就创建新表
*/
public function gainTable()
{
//获取参照表是否存在
$tableOld = Db::query("SHOW TABLES LIKE '$this->prefix$this->tableOld'");
//获取新表是否存在
$isTable = Db::query("SHOW TABLES LIKE '$this->prefix$this->tableNew'");
if (!empty($tableOld) && empty($isTable) && !empty($this->tableNew)) {
//创建表
$this->createTable();
}
}
/**
* Created by
* User: xyz
* Date: 2024/2/19
* Time: 9:42
* Brief: 简介
* docs: 分表不存在,进行复制主表,创建分表表
* @return mixed
*/
protected function createTable(): mixed
{
//获取建表语句
$result = Db::query("SHOW CREATE TABLE $this->prefix$this->tableOld");
$sql = array_pop($result[0]);
$sql = preg_replace(
['/CREATE TABLE `(\w+)`/', '/AUTO_INCREMENT=(\d+)/'],
["CREATE TABLE `$this->prefix$this->tableNew`", "AUTO_INCREMENT=$this->autoIncrement"],
$sql
);
//创建新表
return Db::execute($sql);
}
}
2:子类继承父类全局模型
<?php
namespace app\admin\model\xxx;
use app\common\model\BackCommonModel;
use think\db\exception\DbException;
use think\facade\Db;
use think\Model;
use think\Paginator;
class Axx extends BackCommonModel
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
protected $connection = 'mysql';
protected $name = 'xxxx';
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected int $autoIncrement = 1;//自增id
//分表类型:years:年月,specific_date:年月日,year:年,month:月,day:日,delivery:数字取模分表
protected string $tableType = 'delivery';
//分表取模数
public static int $deliveryNumber = 0;
//分表取模倍数
public static int $deliveryMultiple = 0;
/**
* Created by
* User: xyz
* Date: 2024/2/23
* Time: 17:44
* Brief: 简介
* docs: 模型初始化
*/
protected static function init()
{
(new Axx())->comInit();
parent::init();
}
/**
* Created by
* User: xyz
* Date: 2024/2/23
* Time: 17:43
* Brief: 简介
* docs: 模型初始化,调用父级自定义魔术方法
*/
public function comInit()
{
parent::__comInit(self::$deliveryNumber, self::$deliveryMultiple);
}
}
3:业务层引用模型
<?php
/**
* UserService.php
* 文件描述
* Created on 2023/6/5 11:41
* Create by xyz
*/
namespace app\admin\service\xxxService;
use app\admin\model\powers\Axx;
use app\common\service\CommonService;
use fast\Random;
use think\facade\Config;
use think\Exception;
use think\facade\Log;
class xxxService extends CommonService
{
/**
* Created by
* User: xyz
* Date: 2023/10/11
* Time: 15:18
* Brief: 简介
* docs: 用户登录
* @param array $param
* @return array
* @throws Exception
* @throws \RedisException
*/
public function userLogin(array $param): array
{
try {
//定义分表取模数
PowersAdmin::$deliveryNumber = 1;
//定义分表取模倍数
PowersAdmin::$deliveryMultiple = 50;
//获取用户信息
$userInfo = PowersAdmin::where('xxxx',$param['xxx'])->find();
} catch (Exception $e) {
Log::error('用户登录错误' . $e->getMessage());
throw new Exception($e->getMessage());
}
}
}
以上就是tp6,tp8的模型分表全部方法啦,只是一个简单的demo,大家可以自己完善更好的。
文章介绍了ThinkPHP框架中如何在模型层实现全局分表功能,包括分表类型的选择、取模计算以及创建新表的逻辑。作者提供了一个简单的模型类和业务层调用的实例。



2290

被折叠的 条评论
为什么被折叠?



