模型
<?php
namespace app\common\model;
use think\Model;
class WalletLog extends Model
{
protected $table = 'tb_walletlog';
protected $pk = 'walletid';
private function getRule(){
return [
'type' => 'mod', // 分表方式
'num' => 3 // 分表数量
];
}
public function saveData($data, $user_id){
return $this->partition(['user_id' => $user_id], "user_id", $this->getRule())->insert($data);
}
public function getAll($where, $field = "*", $user_id){
return $this->partition(['user_id' => $user_id], "user_id", $this->getRule())->where($where)->field($field)->select();
}
}控制器//分表测试
public function walletSave(){
$WalletLogModel = new WalletLog();
for($i = 1; $i < 1000; $i++){
$data = [
'user_id' => $i,
'add_time' => time(),
'type' => 1,
'num' => 1
];
$WalletLogModel->saveData($data, $i);
}
echo 'success';die;
}
public function getWallet(){
$uid = request()->get('user_id',0);
if($uid){
$WalletLogModel = new WalletLog();
$user = $WalletLogModel->getAll( ['user_id'=>$uid],'*', $uid);
return json($user);
}
}分表设置$rule分了3个表,建表tb_walletlog_1,tb_walletlog_2,tb_walletlog_3.CREATE TABLE `tb_walletlog` (
`walletid` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '成就id',
`user_id` INT(11) NOT NULL COMMENT '用户id',
`add_time` INT(10) DEFAULT NULL COMMENT '时间',
`type` INT(1) DEFAULT NULL COMMENT '类型 1消费 2收入 3充值',
`num` DOUBLE(20,3) NOT NULL COMMENT '数量',
PRIMARY KEY (`walletid`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='记录';根据官方文档来的,一次成功了。 