我花了最后几个小时试图找到答案,但根本没有获得。我觉得它最终会变得很简单,我不知道。这部分代码当然是不正确的,但我想展示我在这里尝试做的事情的想法。我目前有一个基础存储库类,其中包含 findAllQueryBuilder 方法,可以减少代码。现在我有大约 10 个存储库类都需要使用 findAllQueryBuilder。此功能在我构建的基本控制器中。主要问题是这个控制器看不到 findAllQueryBuilder 因为你可以看到它接受参数来确定需要哪个数据位置。我已经在我的代码中的另一个位置使用接口完成了此操作,但是那是一个类,我也将把该示例放在这里。
public function listAction(Request $request, RepositoryTypeInterface $repositoryName, $table, $sql, $route)
{
$filter = $request->query->get('filter');
$repository = "Bundle:$repositoryName";
$qb = $this->getDoctrine()
->getRepository($repository, 'tenant')
->findAllQueryBuilder($filter, $table, $sql);
正如您在下面看到的那样,我将类型设置为接口,以确保它知道它将是那个时候。
public function newAction(Request $request, EntityTypeInterface $controllerType, formType, $repository, $routeName)
{
然后在子类控制器中实例化它
public function newAction(Request $request, EntityTypeInterface $controllerType = null, $formType = ContactType::class, $repository = 'Contact', $routeName = 'api_contacts_show')
{
$controllerType = new Contact();
return parent::newAction($request, $controllerType, $formType, $repository, $routeName);
}
所以上面的第一个例子让我尝试了同样的事情,但是我不确定如何将它应用于这种情况。因为“Bundle:$repository”是一个字符串,而使用的参数是一个字符串,它不是一个实体,所以当我只需要它的功能时,创建一个实例是没有意义的。我只需要某种方式来访问此功能。任何想法都会奏效,但我觉得我错过了一些简单的东西。
这是我得到的确切错误,在 \Doctrine\Common\Persistence\ObjectRepository 中找不到方法“findAllQueryBuilder” 在主题类中找不到引用的方法。
我确信有一种方法可以应用这个概念来解决缺乏“看到”功能的问题,但是到目前为止我并不完全确定。提前谢谢大家。
编辑:我正在使用一个为其他控制器设置它的基本控制器,即:cantactscontrollor 类,它使用映射到 contactrepository 类的 ContactEntity 类,该类是 findAllQueryBuilder 所在的 baserepository 类的子类,我不是确定如何映射。