首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在扩展\Doctrine\DBAL\Connection的Connection中调用服务

在扩展\Doctrine\DBAL\Connection的Connection中调用服务
EN

Stack Overflow用户
提问于 2019-10-08 23:59:10
回答 1查看 264关注 0票数 0

我正在尝试创建到数据库的动态连接。

为此,我有:

代码语言:javascript
复制
// 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的有效负载中获得了DB名称,如下所示:

代码语言:javascript
复制
// 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个参数。

EN

回答 1

Stack Overflow用户

发布于 2019-10-09 00:40:55

我认为您可以将其注入到构造函数中,如: // App/Services/Config/Database/Connection.php

代码语言:javascript
复制
<?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注入:

代码语言:javascript
复制
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中

代码语言:javascript
复制
  connection.service:
      class: App\Service\ConnectionService

    App\Service\Config\Database\Connection:
      calls:
        - ['setConnectionService', ["@connection.service"]]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58290034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档