3

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",
Community
  • 1
  • 1
Ghalnas
  • 194
  • 1
  • 8

1 Answers1

-1

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.

Ghalnas
  • 194
  • 1
  • 8
  • Glad you got it working but your original setup should have worked. Can't see how you could have possibly gotten an em injected instead of the dbal connection. Are you actually pulling my_repo from the container? Almost seems like you were using the new operator. – Cerad Feb 20 '16 at 19:16
  • Nop, not any **new** anywhere. I didn't do anything but what I've showed here. My Controller was only calling my repo's method. I only wanted to inject the **abstraction layer** using the services.yml, so I didn't write any php code but the one inside my Repo. But regarding my answer to myself, I fill kinda dumb of having tried with all this "complexity" while I could've done it in litterally one php line. – Ghalnas Feb 20 '16 at 20:28
  • Fair enough. I don't suppose the is any chance that you used '@database_connection' instead of '@doctrine.dbal.default_connection'? I know it does not really matter but if you are sticking to straight sql then you don't really need the entity manager or to extend from the EntityRepository. Just a bit of a mystery on a boring afternoon. – Cerad Feb 20 '16 at 20:34
  • I tryed both, none worked. Anyway, I'm ok with symfony doing some witchcraft that's way above my skills as long as I can continue working :D – Ghalnas Feb 20 '16 at 20:40