在Symfony 2 / Doctrine 2中使用DBAL连接的自定义存储库? [英] Having a custom repository with DBAL connection in Symfony 2 / Doctrine 2?

查看:94
本文介绍了在Symfony 2 / Doctrine 2中使用DBAL连接的自定义存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to inject default DBAL connection into a custom repository associated with a entity so I can do some raw sql query.

在services.mxl

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>

在我的存储库类DocumentRepository.php

In my repository class DocumentRepository.php

class DocumentRepository extends EntityRepository {

    protected $conn;

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

但是我收到这个错误:


可追究的致命错误:参数3传递给
Acme\Bundle\Repository\DocumentRepository :: __ construct()必须是一个
的Doctrine的实例$ DB
/ b / b / b / b / b / b / b / b / b / b / src / Acme / Bundle / Repository / DocumentRepository.php第18行

Catchable Fatal Error: Argument 3 passed to Acme\Bundle\Repository\DocumentRepository::__construct() must be an instance of Doctrine\DBAL\Connection, none given, called in /project/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php on line 689 and defined in /project/src/Acme/Bundle/Repository/DocumentRepository.php line 18

可以帮我吗?

推荐答案

您可以从EntityRepository类的$ _em属性达到连接,这样可以做到这一点;

You can reach the connection from $_em attribute of EntityRepository class so here is how you could do it;

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

这篇关于在Symfony 2 / Doctrine 2中使用DBAL连接的自定义存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆