doujiazong0322 2019-06-22 20:53
浏览 93

为什么我不能在symfony4中的任何类中使用服务。 DependencyInjection

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')
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog