Koper
Koper

Reputation: 123

Config Symfony2 and Doctrine

I am new to Symfony2 and Doctrine as well actually anyway. I am trying to config my Doctrine so i can use it in my Symfony2 project.

I have created a DatabaseRepository file where I am doing my connection. When i execute this file i get an error:

Catchable fatal error: Argument 1 passed to DatabaseRepository::__construct() must be an instance of Doctrine\DBAL\Connection, none given, called in app/cache/dev/appDevDebugProjectContainer.php on line 2363

My DatabaseRepository file:

<?php

namespace Acme\DemoBundle\Doctrine;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Psr\Log\LoggerInterface;

class DatabaseRepository
{
    /**
     * @var Connection
     */
    protected $db;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @param Connection $connection
     * @param LoggerInterface $logger
     */
    public function __construct(
        Connection $connection,
        LoggerInterface $logger
    ) {
        $this->db = $connection;
        $this->logger = $logger;
    }
}

doctrine sits under Vendor directory. I am using use to add the connection but this still will not work.

OK guys sorry I think this question is incomplete I will all these.

This is where I call my DatabaseRepository:

namespace Acme\DemoBundle\Repository;

use Acme\DemoBundle\Doctrine\DatabaseRepository;

class TestRepo {

    public $doctrine;

    public function __construct(
        DatabaseRepository $databaseRepository
    ){
        $this->doctrine = $databaseRepository;
    }


    public function test()
    {

    }

} 

And the Below are the services:

<!-- Doctrine -->
<service id="database_repository"
         class="Acme\DemoBundle\Doctrine\DatabaseRepository">
        <argument type="service" id="Doctrine\DBAL\Connection" />
        <argument type="service" id="Psr\Log\LoggerInterface" />
</service>

<service id="test_repo"
         class="Acme\DemoBundle\Repository\TestRepo">
    <argument type="service" id="database_repository" />
</service>

As soogested in comments below I added arguments used to my database_repository service.

But now I get this error:

Fatal error: Uncaught exception 'Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException' with message 'The service "jdatabase_repository" has a dependency on a non-existent service "doctrine\dbal\connection"

Upvotes: 1

Views: 125

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15686

Your database_repository configuration doesn't contain any argument sections which are mapping for constructor parameters. In short, you don't provide requered parameters which are DatabaseRepository and LoggerInterface instances.

There's a database_connection service which represents the first one. More info about how to use DBAL in Symfony here

So to add it to you conf file you should modify it like this:

<service id="database_repository"
         class="Acme\DemoBundle\Doctrine\DatabaseRepository">
    <argument type="service" id="database_connection" />
</service>

You have also to add LoggerInterface instance but that's out of Symfony so you have to create a service for that in first place.

Please also refer to Service Container documentation in order to understand how services work in Symfony2

Upvotes: 2

Related Questions