Koper
Koper

Reputation: 123

Symfony2 Doctrine connection issue

I am learning Symfony2 and trying to Connect to doctrine dbal. But i am encountering an error which up to this point i cannot solve.

Error message:

Catchable fatal error: Argument 1 passed to Doctrine\DBAL\Connection::__construct() must be of the type array, none given, called in /Users/toma/Dev/api/app/cache/dev/appDevDebugProjectContainer.php on line 2313 and defined in /Users/tom/Dev/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php on line 192

This is where I am calling the Doctrine/DBAL/Connection:

<?php

namespace API\Test\TestDoctrine\Repository;

use API\TestBundle\TestDoctrine\DatabaseRepository;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;


class TestRepo {

    public $doctrine;

    public function __construct(
        DatabaseRepository $databaseRepository,
        Connection $connection,
        LoggerInterface $logger
    ){
        $this->doctrine = $databaseRepository;
    }


    public function test()
    {
        $test = 'Hey';
        return $test;
    }


} 

I have creted this file as a service and injecting it into my repository where i want to query my DB. I tried google this issue but unfortinally cannot find an answer.

Services.xml

<service id="api.dto.template.connection"
         class="Doctrine\DBAL\Connection">
</service>

<service id="api.dto.template.logger"
         class="Psr\Log\LoggerInterface">
</service>

<service id="api.testdoctrine.database_repository"
         class="Api\TestBundle\TestDoctrine\DatabaseRepository">
    <argument type="service" id="japi.dto.template.connection" />
    <argument type="service" id="api.dto.template.logger" />
</service>

<service id="api.testdoctrine.repository.test_repo"
         class="API\TestBundle\TestDoctrine\Repository\TestRepo">
    <argument type="service" id="api.testdoctrine.database_repository" />
</service>

Upvotes: 1

Views: 869

Answers (1)

Stev
Stev

Reputation: 1112

I don't know what you are trying to do there, but it doesn't have any logic to me.

Doctrine 2 works out of the box in Symfony 2 Standard Edition. All you need to do is to add the connection parameters in parameters.yml

#app/config/parameters.yml
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: db_name
database_user: db_user
database_password: db_password

And in your controllers you can access your entities repositories like this:

$results = $em->getRepository('YourAppBundle:EntityName')->yourRepositoryMethod();

Check this for more information: http://symfony.com/doc/current/book/doctrine.html

Upvotes: 2

Related Questions