This commit is contained in:
2020-08-31 13:49:40 +08:00
parent 50cc15ea4b
commit 801c7dafc0
5 changed files with 125 additions and 10 deletions
+1 -1
View File
@@ -766,7 +766,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
*/
public static function setDatabaseConnect($bsName)
{
return Snowflake::$app->{$bsName};
return Snowflake::get()->db->instance($bsName);
}
/**
+23
View File
@@ -14,8 +14,10 @@ use Database\Mysql\Schema;
use Database\Orm\Select;
use Exception;
use PDO;
use Snowflake\Config;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
/**
@@ -185,6 +187,27 @@ class Connection extends Component
}
/**
* @param $name
* @return Connection
* @throws ConfigException
*/
public function instance($name)
{
$config = Config::get('databases.' . $name, true);
$this->cds = $config['cds'];
$this->username = $config['username'];
$this->password = $config['password'];
$this->tablePrefix = $config['tablePrefix'];
$this->slaveConfig = $config['slaveConfig'];
return $this;
}
/**
* @return PDO
* @throws Exception
+66
View File
@@ -0,0 +1,66 @@
<?php
defined('CONNECT_HOST') or define('CONNECT_HOST', '');
defined('CONNECT_USER') or define('CONNECT_USER', '');
defined('CONNECT_PASS') or define('CONNECT_PASS', '');
return [
'cache' => [
'file' => [
'path' => strpos(null, 'data')
],
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'prefix' => 'cache_',
'auth' => '',
'databases' => '0',
'timeout' => -1,
'read_timeout' => -1,
],
],
'databases' => [
'db' => [
'id' => 'db',
'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS,
'tablePrefix' => 'aircraftwar_',
'maxNumber' => 100,
'slaveConfig' => [
'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS
],
],
'server' => [
'id' => 'server',
'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS,
'tablePrefix' => 'master_',
'maxNumber' => 100,
'slaveConfig' => [
'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS
],
],
'game' => [
'id' => 'game',
'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS,
'maxNumber' => 100,
'tablePrefix' => 'game_',
'slaveConfig' => [
'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS
],
],
]
];