【发布时间】:2016-06-02 07:38:48
【问题描述】:
我会尽量清楚:我需要在我的一个存储库中使用 SQL。我在互联网上读到我必须使用 Doctrine 的 DBAL 才能使其工作。我在这里举了一个例子: How do you access Doctrine DBAL in a Symfony2 service class? 或那里http://inchoo.net/dev-talk/doctrine-dbal-with-symfony2/
这是我的repo:
class myRepository
{
private $conn;
public function __construct(Connection $conn){
$this->conn = $conn;
}
public function getStuff(){
$sql = "SELECT * FROM stuff";
return $this->conn->fetchAll($sql);
}
}
问题出在构造函数中的 $conn 参数上。 我正在尝试获取 DBAL\connection 对象并获取 EntityManager。
这是我的 Bundle/Resources/config/services.yml
services:
my_repo:
class: MyBundle\Repository\MyRepository
arguments: ["@doctrine.dbal.default_connection"]
和 app/config.yml 学说部分:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
最后,错误:
可捕获的致命错误:传递给 MyBundle\Repository\MyRepository::__construct() 的参数 1 必须是 Doctrine\DBAL\Connection 的实例,给定的 Doctrine\ORM\EntityManager 实例,在 D:\dev\php\ 中调用MyProject\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRepositoryFactory.php 在第 68 行并定义了 500 Internal Server Error - ContextErrorException
有人知道这个错误,以及如何解决它吗?
注意:我已经尝试不在 app/config.yml 中命名我的连接,因为我猜它应该为多个连接保留。我还尝试使用“@database_connection”作为我的 repo 构造函数的参数,还尝试了“@doctrine.dbal”,而我的连接没有命名。
Nb2:如果需要,这是我的 composer.json 的一部分
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.6",
【问题讨论】: