2013-02-22 24 views
3

我有服務在我的services.xmlSymfony的2傳球達陣在service.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'); 

回答

12

Еhis比如應明確原則:

* .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> 
+0

這應該是被接受的答案,更具有示範性。 – chalasr 2016-06-29 19:37:22

1

好了,找到了答案

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