Symfony 2在service.xml中传递数组

前端之家收集整理的这篇文章主要介绍了Symfony 2在service.xml中传递数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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>

但得到错误

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

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

<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');

解决方法

这个例子应该澄清原则:

在* .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>

猜你在找的XML相关文章