国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

如何為某個(gè)應(yīng)用配置獨(dú)立數(shù)據(jù)庫?

fudaoji

如果我們想為某個(gè)應(yīng)用單獨(dú)配置數(shù)據(jù)庫,可以如下操作(舉例為微信平臺(tái)(wechat)應(yīng)用單獨(dú)配置數(shù)據(jù)庫):

1、編輯wechat/config/thinkorm.php

<?php

$system_config = config('thinkorm');

$default = $system_config['connections'][$system_config['default']];
$hostname = '鏈接地址';
$hostport = '端口';
$database = '數(shù)據(jù)庫名稱';
$username = '數(shù)據(jù)庫賬戶';
$password = '數(shù)據(jù)庫密碼';
$charset = '編碼';
$prefix = '表前綴';

$system_config['connections']['wechat'] = [
    // 數(shù)據(jù)庫類型
    'type' => 'mysql',
    // 服務(wù)器地址
    'hostname' => $hostname,
    // 數(shù)據(jù)庫名
    'database' => $database,
    // 數(shù)據(jù)庫用戶名
    'username' => $username,
    // 數(shù)據(jù)庫密碼
    'password' => $password,
    // 數(shù)據(jù)庫連接端口
    'hostport' => $hostport,
    // 數(shù)據(jù)庫連接參數(shù)
    'params' => [],
    // 數(shù)據(jù)庫編碼默認(rèn)采用utf8
    'charset' => $charset,
    // 數(shù)據(jù)庫表前綴
    'prefix' => $prefix,
    // 斷線重連
    'break_reconnect' => true,
    // 關(guān)閉SQL監(jiān)聽日志
    'trigger_sql' => false,
    // 開啟自動(dòng)寫入時(shí)間戳字段
    'auto_timestamp' => true,
];

return $system_config;

2、在應(yīng)用的配置目錄(wechat/config)下新增bootstrap.php:

<?php

return [
    plugin\wechat\app\bootstrap\ThinkOrm::class
];

3、新建文件夾wechat/app/bootstrap,并在文件夾下創(chuàng)建類文件 ThinkOrm.php:

<?php 

namespace plugin\wechat\app\bootstrap;

use Throwable;
use Webman\Bootstrap;
use Workerman\Timer;
use think\Paginator;
use think\facade\Db;
use think\db\connector\Mysql;

class ThinkOrm implements Bootstrap
{
    // 進(jìn)程啟動(dòng)時(shí)調(diào)用
    public static function start($worker)
    {
        $config = config('plugin.wechat.thinkorm');
        $default = $config['default'] ?? false;
        $connections = $config['connections'] ?? [];
        // 配置
        Db::setConfig($config);
        // 維持mysql心跳
        if ($worker) {
            Timer::add(55, function () use ($connections, $default) {
                if (!class_exists(Mysql::class, false)) {
                    return;
                }
                foreach ($connections as $key => $item) {
                    if ($item['type'] == 'mysql') {
                        try {
                            if ($key == $default) {
                                Db::query('select 1');
                            } else {
                                Db::connect($key)->query('select 1');
                            }
                        } catch (Throwable $e) {}
                    }
                }
                Db::getDbLog(true);
            });
        }
        Paginator::currentPageResolver(function ($pageName = 'page') {
            $page = request()->input($pageName, 1);
            if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int)$page >= 1) {
                return (int)$page;
            }
            return 1;
        });
    }
}

4、修改應(yīng)用的模型類$connection屬性,為了修改方便我們習(xí)慣為每個(gè)應(yīng)用也建立個(gè)模型基類,然后只要修改基類中的屬性即可。修改wechat/app/model/Base.php:

// 設(shè)置當(dāng)前模型的數(shù)據(jù)庫連接
protected $connection = 'wechat';
1483 0 2
0個(gè)評(píng)論

年代過于久遠(yuǎn),無法發(fā)表評(píng)論

fudaoji

50
積分
0
獲贊數(shù)
0
粉絲數(shù)
2022-09-21 加入
??