在Symfony 2/Doctrine 2中有一个具有DBAL连接的自定义存储库?

在Symfony 2/Doctrine 2中有一个具有DBAL连接的自定义存储库?,doctrine,doctrine-orm,symfony-2.1,Doctrine,Doctrine Orm,Symfony 2.1,我正在尝试将默认DBAL连接注入与实体关联的自定义存储库,以便执行一些原始sql查询 In services.mxl <service id="acme.repository.document" class="Acme\Bundle\Repository\DocumentRepository"> <argument type="service" id="doctrine.orm.entity_manager" /> <argument>

我正在尝试将默认DBAL连接注入与实体关联的自定义存储库,以便执行一些原始sql查询

In services.mxl

<service id="acme.repository.document" class="Acme\Bundle\Repository\DocumentRepository">
      <argument type="service" id="doctrine.orm.entity_manager" />
      <argument>Acme\Bundle\Entity\Document</argument>
      <argument type="service" id="database_connection" />
</service>
但我得到了这个错误:

可捕获致命错误:参数3传递给 Acme\Bundle\Repository\DocumentRepository::\uu构造()必须是 条令\DBAL\Connection实例,未给定,已调用 /上的project/vendor/doctrine/orm/lib/doctrine/orm/EntityManager.php 第689行,并在中定义 /project/src/Acme/Bundle/Repository/DocumentRepository.php第18行


你能帮我吗?

在ORM中的完整报告。您仍然可以定义自己的服务(也可以是存储库),然后使用该服务。这是ORM的一个限制,因为条令在内部不使用服务位置或依赖注入容器。

条令ORM中的EntityRepository
。您仍然可以定义自己的服务(也可以是存储库),然后使用该服务。这是ORM的一个限制,因为条令在内部不使用服务位置或依赖项注入容器。

问题是您没有传递正确的连接实例,请尝试以下操作:

use Doctrine\DBAL\DriverManager;

class DocumentRepository extends EntityRepository 
{

    protected $conn;

    public function __construct($em, $class)
    {
        $this->conn = $this->myHandleConnection();
         parent::__construct($em,$class);
    }

    public function myHandleConnection()
    {
        $config = new \Doctrine\DBAL\Configuration();
        $connectionParams = array(
                'dbname' => 'MY_DB',
                'user' => 'root',
                'password' => 'root',
                'host' => 'localhost',
                'driver' => 'pdo_mysql',
                'charset' => 'utf8',
                );
        $conn = DriverManager::getConnection($connectionParams, $config);
        return $conn;
    }
}

问题是您没有传递正确的连接实例,请尝试以下操作:

use Doctrine\DBAL\DriverManager;

class DocumentRepository extends EntityRepository 
{

    protected $conn;

    public function __construct($em, $class)
    {
        $this->conn = $this->myHandleConnection();
         parent::__construct($em,$class);
    }

    public function myHandleConnection()
    {
        $config = new \Doctrine\DBAL\Configuration();
        $connectionParams = array(
                'dbname' => 'MY_DB',
                'user' => 'root',
                'password' => 'root',
                'host' => 'localhost',
                'driver' => 'pdo_mysql',
                'charset' => 'utf8',
                );
        $conn = DriverManager::getConnection($connectionParams, $config);
        return $conn;
    }
}

数小时后,继续搜索最佳答案。我发现的答案是最好的(在我看来,还有我的用例)

来源


数小时后,继续搜索最佳答案。我发现的答案是最好的(在我看来,还有我的用例)

来源


您可以从EntityRepository类的$_em属性访问连接,下面是您可以如何实现的方法

$connection = $this->_em->getConnection();

您可以从EntityRepository类的$_em属性访问连接,下面是您可以如何实现的方法

$connection = $this->_em->getConnection();
$connection = $this->_em->getConnection();