Невозможно получить Doctrine EntityManager с автопроводом в Symfony

Ну, я схожу с ума, есть кое-что, чего я не получаю с Symfony (3.3).
Я прочитал эти ресурсы:
http://symfony.com/doc/current/service_container.html
http://symfony.com/doc/current/service_container/3.3-di-changes.html
и другие, которые я не могу опубликовать, потому что мне нужно 10 репутации.
Я также прочитал много статей / сообщений stackoverflow, но без удачи.

Я попытался настроить свои службы, и это почти работает, просто EntityManager имеет значение null, поэтому я не могу вызвать ->getRepository() в теме. Ошибка происходит на линии, как это на моем заводе:

$databaseTournament = $this->em->getRepository('AppBundle:Tournament')
->find($tournament);

«Вызов функции-члена getRepository () в null»

Кажется, что службы находят правильно, но никогда не внедряют EntityManager на заводе. Я пытался настроить вещи явно в services.yml, но мне так и не удалось заставить его работать.

Я пытаюсь понять это со вчерашнего дня, но я действительно запутался. Не могли бы вы помочь мне ? Заранее спасибо ! 🙂

Вот мои файлы:

services.yml:

parameters:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']

EntryFactory:

<?php
namespace AppBundle\Factory;

use AppBundle\Entity\Entry;
use AppBundle\Entity\Team;
use AppBundle\Entity\Tournament;
use AppBundle\Exception\InvalidEntryArgumentException;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;

class EntryFactory
{
private $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

public function createEntry($tournament, $team, $eliminated = false)
{
$entry = new Entry();

if ($tournament instanceof Tournament) {
$entry->setTournament($tournament);
}
elseif (is_int($tournament) && $tournament >= 0) {
$databaseTournament = $this->em->getRepository('AppBundle:Tournament')->find($tournament);
if (is_null($databaseTournament)) {
throw new EntityNotFoundException('Tournament (id:'.$tournament.') not found.');
}
else {
$entry->setTournament($databaseTournament);
}
}
else {
throw new InvalidEntryArgumentException('Could not determine the Tournament argument.');
}

if ($team instanceof Team) {
$entry->setTeam($team);
}
elseif (is_int($team) && $team >= 0) {
$databaseTeam = $this->em->getRepository('AppBundle:Team')->find($team);
if (is_null($databaseTeam)) {
throw new EntityNotFoundException('Team (id:'.$team.') not found.');
}
else {
$entry->setTeam($databaseTeam);
}
}
else {
throw new InvalidEntryArgumentException('Could not determine the Team argument.');
}$entry->setEliminated($eliminated);
return $entry;
}
}

EntryController:

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Entry;
use Doctrine\ORM\EntityNotFoundException;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationList;
use AppBundle\Factory;

class EntryController extends FOSRestController
{
/**
* @ApiDoc(
*     resource=true,
*     section="Entries",
*     description="Creates a new entry.",
*     statusCodes={
*          201="Returned when created.",
*          400="Returned when a violation is raised by validation.",
*          404="Returned when a team or a tournament is not found."*     }
* )
*
* @Rest\Post(
*     path="/entries",
*     name="app_entry_create"* )
* @Rest\View(statusCode=201)
* @Rest\RequestParam(
*     name="tournamentId",
*     requirements="\d+",
*     nullable=false,
*     description="The id of the tournament in which the team enters."* )
* @Rest\RequestParam(
*     name="teamId",
*     requirements="\d+",
*     nullable=false,
*     description="The id of the team entering the tournament."* )
*
* @param $tournamentId
* @param $teamId
* @param ConstraintViolationList $violationList
* @param Factory\EntryFactory $entryFactory
* @return Entry|View
* @throws EntityNotFoundException
* @internal param $id
* @internal param Entry $entry
*/
public function createEntryAction($tournamentId, $teamId, ConstraintViolationList $violationList,
Factory\EntryFactory $entryFactory)
{
if (count($violationList)) {
return $this->view($violationList, Response::HTTP_BAD_REQUEST);
}
$entry = $entryFactory->createEntry($tournamentId,$teamId);
$em->persist($entry);
$em->flush();
return $entry;
}
}

редактировать:

Я должен добавить, что я пробовал это, и это тоже не работает:

в services.yml:

AppBundle\Factory\EntryFactory:
public: true
arguments: ['@doctrine.orm.entity_manager']

в EntryFactory:

public function __construct(EntityManager $em)
{
$this->em = $em;
}

редактировать 2:

Я также пытался отключить все новое: все та же ошибка

AppBundle\Factory\EntryFactory:
autowire: false
autoconfigure: false
public: true
arguments:
$em: '@doctrine.orm.entity_manager'

То же самое с отключением папки в конфигурации по умолчанию:

AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Entity,Repository,Tests,Factory}'

2

Решение

Ты пишешь Service в вашем app/config/service.yml и добавить Factory:

services:
AppBundle\:
resource: '../../src/AppBundle/*'
public: true
exclude: '../../src/AppBundle/{Entity,Factory,Repository,Tests}'

AppBundle\Factory\EntryFactory
arguments: ['@doctrine.orm.entity_manager']

И у нас есть пропуск EntityManager в __construct учебный класс EntryFactory выглядит как:

class EntryFactory
{
private $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
................................................................
................................................................
0

Другие решения

Альтернатива силе Внедрение зависимостей в Symfony 3.3 или позже, без необходимости явно определять менеджера сущностей для каждой услуги. Можете добавить псевдоним желаемой службы менеджера сущностей.

#app/config/services.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: false

AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'

AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']

Doctrine\ORM\EntityManagerInterface: '@doctrine.orm.entity_manager'

#custom services below here
#...

Это позволит Symfony определить Doctrine\ORM\EntityManagerInterface как сервис, который вы хотите внедрить.

use Doctrine\ORM\EntityManagerInterface;

class EntryFactory
{
private $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

//...
}

Конечный результат будет в конфигурации ваших услуг как:

return $this->services['AppBundle\Factory\EntryFactory'] = new \AppBundle\Factory\EntryFactory(${($_ = isset($this->services['doctrine.orm.default_entity_manager']) ? $this->services['doctrine.orm.default_entity_manager'] : $this->load('getDoctrine_Orm_DefaultEntityManagerService.php')) && false ?: '_'});

Примечание. Обязательно прогрейте кеш Symfony, чтобы обеспечить новый сервис
объявления созданы.

php composer.phar install

или же

php composer.phar install --no-scripts
php bin/console --env=dev cache:clear --no-warmup`
php bin/console --env=dev cache:warmup
0