symfony - 在 Connection 中调用服务,它扩展了 \Doctrine\DBAL\Connection
问题描述
我正在尝试创建与数据库的动态连接。
为此,我有:
// 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'];
}
}
这两个功能独立工作。但是如何在 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"]]
推荐阅读
- nearprotocol - 交易/操作的气体限制是多少?
- c++ - 从 CMake 在 Eigen 中设置最大静态对齐
- excel - 如何使循环检查,总结单元格值并停在正确的位置
- c - c中的变量随机改变值
- racket - 如何在 Racket 中制作 HTML 表格边框
- vb.net - 如何计算已在 VB.NET 中的 datagridview 中检查的复选框
- microservices - jhipster 注册中心/控制中心向网关应用程序发送管理请求导致 401 未授权
- html - 为什么不使用填充作为显示?
- python - 我可以将 python 服务器与 kotlin 客户端连接起来吗?
- java - 我如何检测是否有人输入了错误的 otp