Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
zqhong committed Jun 13, 2018
1 parent a9751da commit f845394
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Database/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ protected function createConnection($driver, $connection, $database, $prefix = '
switch ($driver) {
case 'mysql':
// 备注:这里将 MySQL 连接重写
return new ZqhongMySQLConn($connection, $database, $prefix, $config);
return new MySQLConnection($connection, $database, $prefix, $config);
case 'pgsql':
return new PostgresConnection($connection, $database, $prefix, $config);
case 'sqlite':
// 备注:这里将 SQLite 连接重写
return new ZqhongSQLiteConn($connection, $database, $prefix, $config);
return new SQLiteConnection($connection, $database, $prefix, $config);
case 'sqlsrv':
return new SqlServerConnection($connection, $database, $prefix, $config);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Database/MySQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace Zqhong\FastdEloquent\Database;

use Illuminate\Database\MySqlConnection as DefaultMySQLConn;
use Illuminate\Database\MySqlConnection as BaseMySQLConn;
use PDO;

/**
* 备注:添加前缀(Zqhong),便于区分默认连接和自定义连接
*
* @package Zqhong\FastdEloquent
*/
class ZqhongMySQLConn extends DefaultMySQLConn
class MySQLConnection extends BaseMySQLConn
{
/**
* Bind values to their parameters in the given statement.
Expand Down
30 changes: 18 additions & 12 deletions src/EloquentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Events\Dispatcher;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Zqhong\FastdEloquent\Database\ConnectionFactory;

/**
Expand All @@ -33,10 +32,9 @@ public function register(Container $container)
$this->capsule = new Manager();

$dbConfig = $container->get('config')->get('database', []);
Collection::make($dbConfig)
->each(function ($config, $dbName) {
$this->addConnection($dbName, $config);
});
foreach ($dbConfig as $dbName => $config) {
$this->addConnection($dbName, $config);
}

$this->capsule->setAsGlobal();
$this->capsule->bootEloquent();
Expand Down Expand Up @@ -91,17 +89,25 @@ protected function setPageResolver(Container $container)

protected function addConnection($dbName, $dbConfig)
{
$eloquentSettings = [
'driver' => Arr::get($dbConfig, 'adapter', 'mysql'),
'host' => Arr::get($dbConfig, 'host', '127.0.0.1'),
'database' => Arr::get($dbConfig, 'name', ''),
'username' => Arr::get($dbConfig, 'user', 'root'),
'password' => Arr::get($dbConfig, 'pass', 'root'),
$setting = [
'driver' => Arr::get($dbConfig, 'adapter'),
'host' => Arr::get($dbConfig, 'host'),
'database' => Arr::get($dbConfig, 'name'),
'username' => Arr::get($dbConfig, 'user'),
'password' => Arr::get($dbConfig, 'pass'),
'charset' => Arr::get($dbConfig, 'charset', 'utf8'),
'collation' => Arr::get($dbConfig, 'collation', 'utf8_general_ci'),
'prefix' => Arr::get($dbConfig, 'prefix', ''),
// PDO options
'options' => Arr::get($dbConfig, 'options', []),
];

$this->capsule->addConnection($eloquentSettings, $dbName);
if ($setting['driver'] == 'mysql') {
$setting['timezone'] = Arr::get($dbConfig, 'timezone');
$setting['modes'] = Arr::get($dbConfig, 'modes');
$setting['strict'] = Arr::get($dbConfig, 'strict');
}

$this->capsule->addConnection($setting, $dbName);
}
}

0 comments on commit f845394

Please sign in to comment.