Symmfony & Sonata: Как получить доступ к EntityManagerInterface из AbstractAdmin расширяется? - PullRequest
0 голосов
/ 22 октября 2019

У меня есть класс, расширяющий AbstractAdmin. Я пытаюсь ввести EntityManagerInterface с помощью:

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityManagerInterface;

    final class TotoAdmin extends AbstractAdmin
    {    
        /**
         * @var EntityManagerInterface
         */
        private $em;

        /**
         * @param EntityManagerInterface $em
         */
        public function __construct(EntityManagerInterface $em)
        {
            $this->em = $em;
        }

Это приводит к пустой странице. Когда я делаю

php bin/console cache:clear

, я получаю сообщение об ошибке:

  Argument 1 passed to App\Admin\ClientAdmin::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, string given, c  
  alled in /var/www/projects/csiquote/var/cache/dev/ContainerF5etCaE/getAdmin_CategoryService.php on line 26 

Что я пропустил?

1 Ответ

0 голосов
/ 22 октября 2019

Вы расширяете класс, который уже имеет __construct

Когда я смотрю его на github Сонаты AbstractAdmin.php Исходный конструктор класса -

 /**
 * @param string $code
 * @param string $class
 * @param string $baseControllerName
 */
public function __construct($code, $class, $baseControllerName)
{
    $this->code = $code;
    $this->class = $class;
    $this->baseControllerName = $baseControllerName;
    $this->predefinePerPageOptions();
    $this->datagridValues['_per_page'] = $this->maxPerPage;
}

Поэтому, если вам нужна дополнительная зависимость, такая как EntityManagerInterface, вы можете скопировать оригиналы и добавить новую в конец. И затем вызовите родительский конструктор тоже

 private $em;

/**
 * @param string $code
 * @param string $class
 * @param string $baseControllerName
 * @param EntityManagerInterface $em
 */
public function __construct($code, $class, $baseControllerName, EntityManagerInterface $em)
{
    parent::__construct($code, $class, $baseControllerName);

    $this->em = $em;
}

Другое дело, что вам нужно настроить его как службу в соответствии с документами

services:
    App\Admin\TotoAdmin:
        arguments: [~, ~, ~, '@doctrine.orm.entity_manager']

Я думаю, чтодолжно работать

...