Ahmed Gaber
Ahmed Gaber

Reputation: 708

Symfony injecting cannot inject doctrine object

I am using Symfony 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

Upvotes: 0

Views: 633

Answers (1)

gatisl
gatisl

Reputation: 1880

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;
    }

}

Upvotes: 3

Related Questions