symfony2 - 在存储库或类中使用多个连接

Rob*_*_UK 10 php database-connection multiple-databases symfony

我有多个连接,我有一个存储库类.我希望存储库类可以访问多个连接.它用于需要从多个数据库主机获取数据的报告.

config.yml

doctrine:
    dbal:
        default_connection:  default
        connections:
          default:
              driver:   "%db_default_driver%"
              host:     "%db_default_host%"
              etc..
          bookings:
              driver:   "%db_readonly_bookings_driver%"
              host:     "%db_readonly_bookings_host%"
              etc ...
          sessions:
              etc..
Run Code Online (Sandbox Code Playgroud)

SalesJournalRepistory.php

namespace Portal\SalesJournalBundle\Repository;

use Doctrine\ORM\EntityRepository;

class SalesJournalRepository extends EntityRepository 
{

  public $connDefault   = null;
  public $connBookings  = null;
  public $connSessions  = null;


  function __construct()
  {
    // this is where I get the error
    $this->connDefault  = $this->getManager('default')->getConnection();
    $this->connBookings = $this->getManager('bookings')->getConnection();
    $this->connSessions = $this->getManager('sessions')->getConnection();
  }

  function testQuery(){
     $sql = "SELECT * FROM testTableBookings LIMIT 10";
     $stmt = $this->connBookings->prepare($sql);
     $results = $stmt->fetchAll();

     print_r($results);
  }

  function testQuery2(){
     $sql = "SELECT * FROM testTableSessions LIMIT 10";
     $stmt = $this->connSessions->prepare($sql);
     $results = $stmt->fetchAll();

     print_r($results);
  }


}
Run Code Online (Sandbox Code Playgroud)

我可以通过控制器使其工作

$connDefault  = $this->getDoctrine()->getManager('default')->getConnection();
$connBookings = $this->getDoctrine()->getManager('bookings')->getConnection();
Run Code Online (Sandbox Code Playgroud)

但我希望能够从存储库运行它.我得到以下错误

PHP Fatal error:  Call to a member function getConnection() on a non-object
Run Code Online (Sandbox Code Playgroud)

我觉得这可能会给人一些线索?然而,我注意到实体,我有点困惑,不知道是不是?

Man*_*eUK 12

一个EntityRepository只应以其拥有实体(和经理)有关-所以请与您的实体管理器混合你的实体库真的不是要走的路.我建议你创建一个服务并注入Doctrine - 然后你可以查询你想要的任何东西.例如 :

配置

[config.yml / services.yml]
services:
   sales_journal:
      class: Acme\DemoBundle\Service\SalesJournal
       arguments: ['@doctrine']
Run Code Online (Sandbox Code Playgroud)

服务

[Acme\DemoBundle\Service\SalesJournal.php]

namespace Acme\DemoBundle\Service;

public class SalesJournal {

    private $connDefault;
    private $connBookings;
    private $connSessions;


    function __construct($doctrine)
    {
        $this->connDefault = $doctrine->getManager('default')->getConnection();
        $this->connBookings = $doctrine->getManager('bookings')->getConnection();
        $this->connSessions = $doctrine->getManager('sessions')->getConnection();
    }

    function testQuery()
    {
        $sql = "SELECT * FROM testTableBookings LIMIT 10";
        $stmt = $this->connBookings->prepare($sql);
        $results = $stmt->fetchAll();

        print_r($results);
    }

    function testQuery2()
    {
        $sql = "SELECT * FROM testTableSessions LIMIT 10";
        $stmt = $this->connSessions->prepare($sql);
        $results = $stmt->fetchAll();

        print_r($results);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后从您的控制器或您想要使用该服务的地方:

// get the service
$sales_journal = $this->get('sales_journal');
// call relevent function
$sales_journal->testQuery();
Run Code Online (Sandbox Code Playgroud)