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