本文介绍了在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);
}
但是我收到这个错误:
可以帮我吗?
推荐答案
您可以从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连接的自定义存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!