10

我有多个连接,并且我有一个存储库类。我希望存储库类能够访问多个连接。它适用于需要从多个数据库主机获取数据的报告。

配置.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..

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);
  }


}

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

$connDefault  = $this->getDoctrine()->getManager('default')->getConnection();
$connBookings = $this->getDoctrine()->getManager('bookings')->getConnection();

但是我希望能够从存储库中运行它。我收到以下错误

PHP Fatal error:  Call to a member function getConnection() on a non-object

我认为这可能会提供一些线索?注入实体但是我有点困惑,不确定是不是?

4

2 回答 2

12

AnEntityRepository应该只关心其拥有的实体(和管理器) - 因此将您的实体存储库与您的实体管理器混合起来确实不是要走的路。我建议你自己创建一个服务并注入 Doctrine - 然后你可以查询任何你想要的。例如 :

配置

[config.yml / services.yml]
services:
   sales_journal:
      class: Acme\DemoBundle\Service\SalesJournal
       arguments: ['@doctrine']

服务

[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);
    }
}

然后从您的控制器或您想使用该服务的任何地方执行以下操作:

// get the service
$sales_journal = $this->get('sales_journal');
// call relevent function
$sales_journal->testQuery();
于 2013-03-11T14:17:56.540 回答
2

您应该将要在控制器中使用的实体管理器设置为 getRepository 方法的第二个参数,如下所示:

$repository = $this->getDoctrine()->getRepository('PortalSalesJournalBundle:SalesJournal', 'bookings');

并在存储库类中访问该实体管理器的连接,如下所示:

namespace Portal\SalesJournalBundle\Repository;

use Doctrine\ORM\EntityRepository;

class SalesJournalRepository extends EntityRepository 
{
    function testQuery(){
        $connection = $this->getEntityManager()->getConnection();
    }
}
于 2013-03-07T22:00:17.840 回答