swoole-framwork/core/init/MyDB.php

48 lines
1.2 KiB
PHP
Raw Normal View History

2024-05-20 11:51:07 +08:00
<?php
namespace Core\init;
use Core\annotations\Bean;
use Illuminate\Database\Capsule\Manager as lvDB;
/**
* @Bean()
* @package Core\init
* @method \Illuminate\Database\Query\Builder table(string $table,string|null $as=null,string|null $connection=null)
*/
class MyDB
{
private $lvDB;
private $dbSource='default';
public function __construct()
{
global $GLOBAL_CONFIGD;
if (isset($GLOBAL_CONFIGD['db'])) {
$this->lvDB = new LvDb();
$configs = $GLOBAL_CONFIGD['db'];
foreach ($configs as $key => $config){
$this->lvDB->addConnection($config,$key);
}
$this->lvDB->addConnection($GLOBAL_CONFIGD['db']['default']);
//设置全局静态访问
$this->lvDB->setAsGlobal();
//启动Eloquent
$this->lvDB->bootEloquent();
}
}
public function __call($methodName, $arguments)
{
//用。。。解构数组
return $this->lvDB::$methodName(...$arguments);
}
public function setDbSource($dbSource)
{
$this->dbSource = $dbSource;
}
public function getDbSource()
{
return $this->dbSource;
}
}