2021年3月

某些场景下很好用的东西,官方没有文档,抽空再整理,下面是使用的基本方法

配置文件定义表结构,文件config/autoload/table.php

<?php
declare(strict_types = 1);

return [
    'reception_id'        => [
        'size'    => 1024 * 5,
        'columns' => [
            'user_id' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'kf_id' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'user_type' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'user_status' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'kf_fd' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'user_fd' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'kf_status' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
        ],
    ],
    'kf_id'        => [
        'size'    => 1024 * 5,
        'columns' => [
            'kf_fd' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'kf_status' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
            'merch_id' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
        ],
    ],
    'user_id' => [
        'size'    => 1024 * 5,
        'columns' => [
            'reception_id' => [
                'type' => Table::TYPE_STRING,
                'size' => 20
            ],
            'user_fd' => [
                'type' => Table::TYPE_INT,
                'size' => 20
            ],
        ],
    ],
// ...
];

事件触发执行初始化内存表,文件app/Listener/BeforeMainServerStartListener.php

//...
    public function listen() : array
    {
        return [
            BeforeMainServerStart::class
        ];
    }

    public function process(object $event)
    {
        $tableConfig = config('table');
        foreach ($tableConfig as $key => $item) {
            TableManager::initialize($key, $item['size']);
            foreach ($item['columns'] as $columnKey => $column) {
                TableManager::get($key)->column($columnKey, $column['type'], $column['size']);
            }
            TableManager::get($key)->create();
        }
    }
//...