1

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>
Koper
  • 123
  • 11
  • What is connection ? can you show us your service :D – MouradK Apr 24 '15 at 09:30
  • 1
    @Koper, did you check this : http://symfony.com/fr/doc/current/book/doctrine.html? You can configure the doctrine2 connection from the parameters part, which are used in the config.yml file, and then injected in the related service. No need for you to configure it. Symfony2 does it for you :) – Ninir Apr 24 '15 at 09:31
  • please see above edits – Koper Apr 24 '15 at 09:34
  • @Ninir thx for your suggestion but can you possibly give me a link to englis version fo this documentation xD – Koper Apr 24 '15 at 09:36
  • Here you go : http://symfony.com/doc/current/book/doctrine.html – Ninir Apr 24 '15 at 09:36
  • If you try to declare your repository as a service try following this http://stackoverflow.com/questions/17228417/symfony-2-creating-a-service-from-a-repository – Stev Apr 24 '15 at 09:37
  • you just want param some connexion ? $this->getDoctrine()->getManager($name); (where $name is your connection name configured in your config.yml ) – MouradK Apr 24 '15 at 09:42
  • If you are using the standard Symfony setup then the service "database_connection" will contain your dbal connection object. Just inject it. Your code is not working because creating a connection object involves a bit more than just new'ing an object. – Cerad Apr 24 '15 at 10:17

1 Answers1

2

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

Ninir
  • 441
  • 4
  • 9
Stev
  • 1,062
  • 11
  • 23
  • Hey I managed to fix all the issues. your suggestion was spot on but i still stuck to the way I was doing things and created a service for parameters.yml where my db details are injected this into DatabaseRepository and Testrepo :) – Koper Apr 24 '15 at 12:00