控制器类看不到存储库类中的方法



我花了几个小时试图找到答案,但根本没有获得。我觉得它最终会成为一些我不知道的简单事情。这部分代码当然是不正确的,但我想展示我在这里尝试做什么的想法。我目前有一个基本存储库类,其中包含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"是一个字符串,使用的参数是一个字符串,它不是一个实体,所以当我只需要它的功能时,创建一个实例没有意义。我只需要一些方法来访问此功能。任何想法都会奏效,但我觉得我错过了一些简单的东西。

这是我得到的确切错误, 方法 'findAllQueryBuilder' 在 \Doctrine\Common\Persistence\ObjectRepository 中找不到 在主题类中找不到引用的方法。

我确信有一种方法可以应用这个概念来解决缺乏"看到"功能的问题,但到目前为止,我并不完全确定。 提前谢谢任何人。

编辑: 我正在使用一个为其他控制器设置的basecontrollor,即:cantactscontrollor类,该类使用映射到contactrepository类的ContactEntity类,该类是findAllQueryBuilder所在的baserepository类的子级,我不确定如何映射它。

public function listAction(Request $request, $repositoryName, $table, $sql, $route)
{
$filter = $request->query->get('filter');
$repository = "TenantBundle:$repositoryName";
/** @var RepositoryTypeInterface $qb */
$qb = $this->getDoctrine()
->getRepository($repository, 'tenant');
$queryResults = $qb->findAllQueryBuilder($filter, $table, $sql);

很抱歉对此的回复晚了,但这是我能够找到的答案。因为我认为这是一件简单的事情,但本质上 Docblock 将 $qb 标记为上面的接口,以确保它知道任何实现它的类都会被接受。现在知道它将是什么类型,错误消息不再存在。还要注意我是如何拆分它的。

最新更新