当前位置:网站首页>Laravel 8 enables the order table to be divided by month level
Laravel 8 enables the order table to be divided by month level
2022-06-29 09:19:00 【amateur12】
Realize the idea :
1. Design basis table orders
2. Create this year through background code 6 Monthly order form :order_202206, This year, 7 Monthly order form :order_202207...
When creating a table, you need to make a judgment , If the table exists , You don't need to create
This background code will be used many times and can be reused , Select write as trait
3. The operations of adding, deleting, modifying and querying after table splitting are the same as before table splitting , But the default is to add, delete, modify and query the current month
trait:
<?php
namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
trait OrderTraits
{
// Sub table or not , Default false, That is, regardless of table
protected $isSplitTable = true;
// The original table
public $originTable;
// surface
public $endTable;
/**
* Suffix parameters
* @var string
*/
protected $suffix = null;
/**
* Year and year parameters :202104
* @var string
*/
public $ym;
public function init(array $attributes = [], $suffix = null)
{
// Default original table
$this->originTable = $this->table;
// Default final table
$this->endTable = $this->table;
$this->ym = Carbon::now()->format('Ym');
//isSplitTable Parameter is true Time division , Otherwise, regardless of the table
if ($this->isSplitTable) {
// Initialize suffix , If it is not transferred, the year / year sub table will be defaulted
$this->suffix = $suffix ?: $this->ym;
}
// Initialize the partition table name and create
$this->setSuffix();
}
/**
* Set table suffix , If the sub table suffix is set , Can be found in service Layer call to generate custom suffix table name ,
* However, this method needs to be called before each operation of the table to ensure the accuracy of the data table
* @param $suffix
*/
public function setSuffix($suffix = null)
{
//isSplitTable Parameter is true Time division , Otherwise, regardless of the table
if ($this->isSplitTable) {
// Initialize suffix , If it is not transferred, the year / year sub table will be defaulted
$this->suffix = $suffix ?: $this->ym;
}
if ($this->suffix !== null) {
//$this->endTable = $this->getTable() . '_' . $suffix;
$this->endTable = $this->originTable . '_' . $this->suffix;
// The final table replaces the table declared in the model as the table used by the sub table
$this->table = $this->endTable;
}
// Invocation time , Create sub table , The format is table_{$suffix}
// When the user-defined suffix is not transmitted ,, The default format of the table by month / year is :orders_202205
// Whether the sub table name is customized or not , Will create a default sub table , Unless you close the call
$this->createTable();
}
/**
* Provide a static method to set the table suffix
* @param string $suffix
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function suffix($suffix = null)
{
$instance = new static;
$instance->setSuffix($suffix);
return $instance->newQuery();
}
/**
* Create a new "table_{$suffix}" And return
* @param array $attributes
* @param bool $exists
* @return object $model
*/
public function newInstance($attributes = [], $exists = false)
{
$model = parent::newInstance($attributes, $exists);
$model->setSuffix($this->suffix);
return $model;
}
/**
* Create sub table , Create if not , If there is, don't deal with
*/
protected function createTable()
{
info("createTable===============", [Schema::hasTable($this->endTable)]);
// Initialize the sub table ,, According to the month and year table, the format is :orders_202205
if (!Schema::hasTable($this->endTable)) {
info(" Create table ==========", [$this->endTable]);
DB::update("create table {$this->endTable} like {$this->originTable}");
}
}
}
stay order Model USES trait:( Any model can use this trait Make monthly tables )
use OrderTraits;
protected $table = "orders";
protected $primaryKey = 'id';
protected $guarded = [];
protected $columns;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
// Initialize split table processing
$this->init();
}effect :

Applicable scenarios by month :
One of the strategies to divide the database and tables is to divide the tables by month , You can do hot and cold data .
such as , This month's data is generally thermal data , The previous month's data is the cold data .
however , There is one thing to be aware of : If a lot of data is written this month , The data of last month or next month Few writes , And there is no need to do some statistics of monthly data in the whole business , It deviates from the starting point of dividing tables due to the large amount of data , therefore , The appropriate strategy should be selected according to the scenario , by comparison , Mold taking is applicable to a wide range of scenarios , The table data can be shared evenly .
边栏推荐
- Wechat applet jump to official account image and text content
- Detailed version of two-stage target detection principle
- 修改exif信息
- Handwriting Redux thunk
- Training kernel switching using GPU
- 微信小程序项目:tab导航栏
- Mqtt second session -- emqx high availability cluster implementation
- Training view (issue temporary storage)
- Augfpn: amélioration de l'apprentissage des caractéristiques à plusieurs échelles pour la détection des cibles
- 使用GPU训练kernel切换
猜你喜欢
随机推荐
Remember to customize the top navigation bar of wechat applet
Redo after JS rotation view (longer full version, can be run)
First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8
手机开户一般哪个证券公司好?究竟网上开户是否安全么?
MT-yolov6训练及测试
调试H5页面-vConsole
cmd进入虚拟机
Verilog reduction operator
手写 redux-thunk
Open3D 最远点采样(FPS)
Analysis of c voice endpoint detection (VAD) implementation process
微信小程序项目:微信小程序页面布局
记微信小程序分享代码片段
Universal target detection based on region attention
SSD改进CFENet
微信小程序自定义多项选择器
Debug H5 page -vconsole
pytorch总结学习系列-操作
微信小程序判断url的文件格式
ISO16000-9建筑产品和家具中挥发性有机物的测试



![[to.Net] C data model, from Entity Framework core to LINQ](/img/98/6a8b295d1465697945e01b8c48ec52.png)





