dongshenjie3055 2016-02-20 18:20
浏览 125
已采纳

使用symfony 3注入doctrine dbal

I'll try to by as clear as possible : I have the need to use SQL in one of my repository. I've read on the internet that I have to use Doctrine's DBAL to make it work. I took one of the exemple of it here : How do you access Doctrine DBAL in a Symfony2 service class? or there http://inchoo.net/dev-talk/doctrine-dbal-with-symfony2/

Here's my 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);
    }
}

The problem is on the $conn parameter in the constructor. I'm trying to get a DBAL\connection object and get an EntityManager instead.

Here's my Bundle/Resources/config/services.yml

services:
    my_repo:
    class: MyBundle\Repository\MyRepository
    arguments: ["@doctrine.dbal.default_connection"]

and the app/config.yml doctrine part :

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

Finally, here's the error :

Catchable Fatal Error: Argument 1 passed to MyBundle\Repository\MyRepository::__construct() must be an instance of Doctrine\DBAL\Connection, instance of Doctrine\ORM\EntityManager given, called in D:\dev\php\MyProject\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRepositoryFactory.php on line 68 and defined 500 Internal Server Error - ContextErrorException

Does anybody have any idea about this error, and how to fix it ?

Nb: I already tried not naming my connection in app/config.yml, because i guess it should be reserved for multiple connections. I also tried to use "@database_connection" as an argument of my repo's constructor, also tried "@doctrine.dbal" while my connection's not named.

Nb2: if needed, here's a part of my composer.json

"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.6",
  • 写回答

1条回答 默认 最新

  • douhuan1901 2016-02-20 18:42
    关注

    Ok, I'll answer to myself : I didn't get where the problem comes from, which would be still interesting to know, but managed to do it in another (way simplier) way :

    class MyRepo extends \Doctrine\ORM\EntityRepository
    {
    
        public function findAllServices(){
            $sql = "SELECT * FROM stuff";
            return $this->getEntityManager()->getConnection()->fetchAll($sql);
        }
    }
    

    I thought I had to use DBAL because I thought it was the only way to query with plain old sql requests... But it wasn't.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的