I have a bundle Symfony4. I need to working with PostgresSQL with ORM Doctrine. I want to call some class(Service) to work with ORM, but i have a problem.
Tried to create a service in services.yaml and create class. Already tried use ContainerBuilder, but have this issue "The service "Event" has a dependency on a non-existent service "doctrine.orm.entity_manager". what am I doing wrong. In class which extends Controller i get EntityManger without problem. How to get EntityManager outside the Controller.
services.yaml
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
Event:
class: App\Core\Event
public: true
arguments: ['@doctrine.orm.entity_manager']
App\Core\Event.php
<?php
namespace App\Core;
use Doctrine\ORM\EntityManagerInterface;
class Event
{
public $entityManager;
public function __construct(EntityManagerInterface $em) {
$this->entityManager = $em;
}
}
Any class where i want to get service
$container = new ContainerBuilder();
$fileLocator = new FileLocator('/home/admin/web/noticer_service/public_html/config');
$loader = new YamlFileLoader($container, $fileLocator);
$loader->load('services.yaml');
$container->compile();
$container->get('Event')