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