【问题标题】:configureActionButtons 中的存储库使用(Sonata Admin Bundle)
【发布时间】:2021-11-09 03:29:32
【问题描述】:

我正在尝试向configureActionButtons 注入教义服务(如果有一种方法可以在没有 Doctrine 的情况下注入 BoothTypeRepository,那也可以),但我找不到注入或教义或 BoothTypeRepository 的方法。

public function configureActionButtons($action, $object = null): array
    {
        $list = parent::configureActionButtons($action, $object);;

        $handles = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager')->getRepository(BoothTypeRepository::class)->findAll();
        
        foreach($handles as $handle) {
            $list['new_' . $handle] = [
                'attr' => $handle,
                'template' => 'CRUD/button_new_booth_type.html.twig'
            ];
        }

        unset($list['new']);

        return $list;
    }

我从上面的代码得到的错误

在渲染模板期间引发了异常 ("Class "App\Repository\BoothTypeRepository" 的子类 "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository" 是 不是有效的实体或映射的超类。”)。

这是BoothTypeRepository 的命名空间 -> namespace App\Repository;

【问题讨论】:

    标签: symfony doctrine-orm sonata-admin sonata


    【解决方案1】:

    答案在构造中: 私有 BoothTypeRepository $boothTypeRepository;

    public function __construct($code, $class, $baseControllerName, BoothTypeRepository $boothTypeRepository)
    {
        parent::__construct($code, $class, $baseControllerName);
    
        $this->boothTypeRepository = $boothTypeRepository;
    }
    

    那就不用教条服务了->

        $boothTypes = $this->boothTypeRepository->findAll();
    

    【讨论】:

      猜你喜欢
      • 2016-09-03
      • 2013-03-06
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      相关资源
      最近更新 更多