我正在尝试创建与数据库的动态连接。
为此,我有:
// App/Services/Config/Database/Connection.php
<?php
namespace App\Service\Config\Database;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class Connection extends \Doctrine\DBAL\Connection
{
public function __construct(
array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
)
{
$company = "api";
$db_name = "speyce_" . $company;
$params['dbname'] = $db_name;
parent::__construct($params, $driver, $config, $eventManager);
}
}
我在JWT的有效负载中获得了数据库名称,如下所示:
// App/Service/ConnectionService.php
<?php
namespace App\Service;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
class ConnectionService
{
public function dbName(
TokenStorageInterface $tokenStorageInterface,
JWTTokenManagerInterface $jwtManager
)
{
$decodedJwtToken = $jwtManager->decode($tokenStorageInterface->getToken());
return $decodedJwtToken['company'];
}
}
这2个功能独立运行。但是,如何在Connection.php中调用服务的方法(connectionService-> dbName)?
我无法在构造函数的参数中调用ConnectionService,因为它仅接受4个参数。
谢谢您的帮助。
我认为您可以将其注入到构造函数中,例如:// App / Services / Config / Database / Connection.php
<?php
namespace App\Service\Config\Database;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use App\Service\ConnectionService;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class Connection extends \Doctrine\DBAL\Connection
{
/**
*@var ConnectionService
*/
protected $connectionService;
public function __construct(
ConnectionService $connectionService
array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
)
{
$company = "api";
$db_name = "speyce_" . $company;
$params['dbname'] = $db_name;
parent::__construct($params, $driver, $config, $eventManager);
$this->$connectionService = $connectionService;
}
}
或使用setter注入:
namespace App\Service\Config\Database;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use App\Service\ConnectionService;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class Connection extends \Doctrine\DBAL\Connection
{
/**
*@var ConnectionService
*/
protected $connectionService;
public function __construct(
array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
)
{
$company = "api";
$db_name = "speyce_" . $company;
$params['dbname'] = $db_name;
parent::__construct($params, $driver, $config, $eventManager);
}
public function setConnectionService(ConnectionService $cs){
$this->connectionService = $cs
}
}
在您的services.yml中
connection.service:
class: App\Service\ConnectionService
App\Service\Config\Database\Connection:
calls:
- ['setConnectionService', ["@connection.service"]]