-1

I have a class extending AbstractAdmin. I try to inject the EntityManagerInterface with :

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

It results in a blanck page. When i do

php bin/console cache:clear

i get the error:

  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 

What did I miss ?

Alexglvr
  • 427
  • 5
  • 18

1 Answers1

0

You are extending a class that already has a __construct

When I look it up on Sonata's github AbstractAdmin.php The original class constructor is

 /**
 * @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;
}

So if you need an extra dependency like EntityManagerInterface you could copy the originals and add the new to the end. And then call the parent constructor too

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

Another thing is that you need to configure it as a service according to the docs

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

I think that should work

Julesezaar
  • 2,658
  • 1
  • 21
  • 21