Service Dependence in Symfony2

I needed to move my model from the controller method, so I got help to change it to a service. The service itself works, but I need to be able to connect to the doctrine and core from within this service. At first I tried to incorporate the doctrine, but this created problems. How can I do this job? I followed the docs and got this code. I have no idea why I got the error below. Thank you for your help.

My config:

CSVImport.php

namespace Tools\TFIBundle\Model; use Doctrine\ORM\EntityManager; class CSVImport { protected $em; public function __construct( EntityManager $em ) { $this->em = $em; } 

application /Config/config.yml

 services: csvimport: class: Tools\TFIBundle\Model\CSVImport arguments: [ @doctrine.orm.entity_manager ] 

action in the controller

 $cvsimport = $this->get('csvimport'); 

MY ERROR

 Catchable Fatal Error: Argument 1 passed to Tools\TFIBundle\Model\CSVImport::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in .../Tools/TFIBundle/Controller/DefaultController.php on line 58 and defined in .../Tools/TFIBundle/Model/CSVImport.php line 12 

EDIT, my working code is:

class of service code with a kernel attached to it

 namespace Tools\TFIBundle\Model; use Doctrine\ORM\EntityManager, AppKernel; class CSVImport { protected $em; protected $kernel; protected $cacheDir; public function __construct( EntityManager $em, AppKernel $k ) { $this->em = $em; $this->kernel = $k; } 
+6
source share
2 answers

Try typing @doctrine.orm.default_entity_manager .

+1
source

On the Internet, I found how to connect to Doalrine DBAL in order to be able to make queries myself. But when I changed my configuration to this:

application /config.yml

 services: csvimport: class: Tools\TFIBundle\Model\CSVImport arguments: [ @doctrine.dbal.connection, @doctrine.orm.entity_manager, @kernel ] 

class definition

 namespace Tools\TFIBundle\Model; use Doctrine\ORM\EntityManager, Doctrine\DBAL\Connection, AppKernel; class CSVImport { protected $c; protected $em; protected $kernel; public function __construct(Connection $c, EntityManager $em, AppKernel $k ) { $this->c = $c; $this->em = $em; $this->kernel = $k; } 

i received error:

 RuntimeException: The definition "csvimport" has a reference to an abstract definition "doctrine.dbal.connection". Abstract definitions cannot be the target of references. 

Any ideas?

+1
source

Source: https://habr.com/ru/post/911406/


All Articles