Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 passing array in service.xml

I have services in my services.xml

<service id="my.connection" class="Doctrine\Bundle\DoctrineBundle\ConnectionFactory">
</service>

<service id="my.main" class="%my.main.class%">
    <call method="foo">
        <argument type="service" id="tmcyc.connection" />
    </call>
</service>

But got error:

Catchable Fatal Error: Argument 1 passed to Doctrine\Bundle\DoctrineBundle\ ConnectionFactory::__construct() must be an array, none given...

How can I pass array with argument? For example:

<service id="my.connection" class="Doctrine\Bundle\DoctrineBundle\ConnectionFactory">
    <argument>[ARRAY]</argument>
</service>

or maybe I'm doing somthing wrong? Because this code works greate:

$connectionFactory = $this->getContainer()->get('doctrine.dbal.connection_factory');
$conn = $this->createConnection($this->conn);
$conn->executeQuery('SET NAMES utf8');
like image 946
Narek Avatar asked Feb 22 '13 10:02

Narek


2 Answers

Еhis example should clarify the principle:

In *.yml

some_id: 
    class: %some.class%
    arguments: 
        - %some.argument%, 
        - [tags: [environment: %kernel.environment%, debug:%kernel.debug%]]

In *.xml

<service id="some_id" class="%some.class%">
    <argument>%some.argument%</argument>
    <argument type="collection">
        <argument key="tags" type="collection">
            <argument key="environment">%kernel.environment%</argument>
            <argument key="debug">%kernel.debug%</argument>
        </argument>
    </argument>
</service>
like image 164
Максим Шатов Avatar answered Nov 20 '22 01:11

Максим Шатов


Ok, found the answer

<service id="my.connection" class="Doctrine\Bundle\DoctrineBundle\ConnectionFactory">
    <argument>%doctrine.dbal.connection_factory.types%</argument>
</service>
like image 1
Narek Avatar answered Nov 20 '22 01:11

Narek