0

我正在使用 Symfony 2,并且正在尝试创建一个自定义服务容器来处理一些计算

我的服务.yml

services:
    my_calculator:
        class:        my_app\HomeBundle\Services\CalculatorService
        arguments:    [doctrine]

CalculatorService 如下

<?php 
namespace my_app\HomeBundle\Services;

use my_app\CategoryBundle\Entity;

class CalculatorService
{
    protected $doctrine;

    public function __construct($doctrine)
    {
        $this->doctrine = $doctrine;
    }

    public function calculate($data=array())
    {
        if(!empty($data))
        {
        }
    }
}

我的控制器功能

public function calculateAction()
{
    $request = $this->get('request');

    $calculator=$this->get('my_calculator');
 }

我的问题是教义对象无法到达构造函数

我试图arguments: [@doctrine]arguments: [@doctrine.orm.entity_manager]

但是一旦我@在 yml 文件中写入,就会出现解析错误

请帮忙

4

1 回答 1

3

This is working code sample for entity manager injection.

services.yml

my_service:
    class: GLS\DemoBundle\MyService
    arguments: ["@doctrine.orm.entity_manager"]

Service class:

class MyService
{
    protected $em;

    public function __construct(\Doctrine\ORM\EntityManager $em)
    {
        $this->em = $em;
    }

}
于 2013-04-28T15:30:53.433 回答