Symmfony & Sonata:如何从AbstractAdmin extends访问EntityManagerInterface?



我有一个扩展AbstractAdmin的类。我尝试用注入EntityManagerInterface

namespace AppAdmin;
use SonataAdminBundleAdminAbstractAdmin;
use SonataAdminBundleDatagridListMapper;
use SonataAdminBundleDatagridDatagridMapper;
use SonataAdminBundleFormFormMapper;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
use SymfonyComponentFormExtensionCoreTypeEmailType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
use SymfonyComponentFormExtensionCoreTypeCountryType;
use SymfonyComponentValidatorConstraintsNotBlank;
use SymfonyComponentValidatorConstraintsLength;
use SymfonyComponentFormFormEvent;
use SymfonyComponentFormFormInterface;
use SymfonyComponentFormFormEvents;
use SymfonyBridgeDoctrineFormTypeEntityType;
use DoctrineORMEntityManagerInterface;
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 AppAdminClientAdmin::__construct() must implement interface DoctrineORMEntityManagerInterface, string given, c  
alled in /var/www/projects/csiquote/var/cache/dev/ContainerF5etCaE/getAdmin_CategoryService.php on line 26 

我错过了什么?

您正在扩展一个已经有__construct 的类

当我在Sonata的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:
AppAdminTotoAdmin:
arguments: [~, ~, ~, '@doctrine.orm.entity_manager']

我认为这应该工作

相关内容

  • 没有找到相关文章

最新更新