0

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

为此,我有:


// 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 个参数。

4

1 回答 1

0

我认为您可以将其注入构造函数中,例如: // 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"]]
于 2019-10-08T16:40:55.147 回答