Symfony 2在service.xml中传递数组

时间:2013-02-22 10:23:26

标签: xml arrays symfony service connection

我的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>

但得到了错误:

  

捕获致命错误:参数1传递给   Doctrine \ Bundle \ DoctrineBundle \ ConnectionFactory :: __ construct()必须   是一个阵列,没有给出......

如何通过参数传递数组? 例如:

<service id="my.connection" class="Doctrine\Bundle\DoctrineBundle\ConnectionFactory">
    <argument>[ARRAY]</argument>
</service>
或者我可能做错了什么?因为这段代码很有用:

$connectionFactory = $this->getContainer()->get('doctrine.dbal.connection_factory');
$conn = $this->createConnection($this->conn);
$conn->executeQuery('SET NAMES utf8');

3 个答案:

答案 0 :(得分:16)

这个例子应该澄清原则:

*。yml

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

*。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>

答案 1 :(得分:1)

好的,找到了答案

<service id="my.connection" class="Doctrine\Bundle\DoctrineBundle\ConnectionFactory">
    <argument>%doctrine.dbal.connection_factory.types%</argument>
</service>

答案 2 :(得分:0)

与参数相同

*。xml

    <parameter key="some.key" type="collection">
        <parameter>parameter 1</parameter>
        <parameter>parameter 2</parameter>
    </parameter>