du521521521 2013-04-28 14:14
浏览 33
已采纳

Symfony注入不能注入主义对象

I am using symphony 2, and i m trying to create a custom service container that handle some calculations

my services.yml

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

CalculatorService is as below

<?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))
        {
        }
    }
}

my controller function

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

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

my problem that doctrine object dosen't get get to the constructor

i have tried to arguments: [@doctrine] and arguments: [@doctrine.orm.entity_manager]

but i get parsing error once i write @ at the yml file

please help

  • 写回答

1条回答 默认 最新

  • dpp34603 2013-04-28 15:30
    关注

    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;
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)