![](/img/trans.png)
[英]How to set custom headers in all methods of a Symfony 4 Controller with FOSRestBundle?
[英]Symfony | How to access the custom methods from the respository in a controller?
我正在建立一个清单,以练习使用Symfony构建Web应用程序。 通过将字符串输入表单并按下提交按钮来工作。 输入存储在数据库中。
我想将数据库中存储的输入返回到网页。
当前,输入存储在数据库中,并且我已经在存储库中编写了DQL查询功能。
我的问题是我无法访问在控制器中创建的方法。
我的存储库中的方法:
/**
* @return Checklist[]
*/
public function getAllItemsForChecklist(): array
{
$qb = $this->createQueryBuilder('c')
->select('item')
->from('checklist', 'x')
->getQuery()
;
return $qb->execute();
}
尝试访问控制器中方法的行(失败):
$items = $this->getDoctrine()
->getRepository(ChecklistRepository::class)
->getAllItemsForChecklist()
;
根据https://symfony.com/doc/master/doctrine.html#querying-for-objects-the-repository上的Symfony文档,这应该可以工作。 但是,找不到方法“ getAllItemsForChecklist()。在我的IDE上给出以下消息:
在\\ Doctrine \\ Common \\ Persistence \\ ObjectRepository中找不到方法'getAllItemsForChecklist'
我不确定为什么它没有读取我指定的存储库类。
如果有人知道如何解决此问题,那么它将在我的存储库中找到我所赞赏的方法。
另外,如果需要任何其他信息,请告诉我我很乐意提供更多信息。
问候,
史蒂夫
欢迎来到StackOverflow!
首先,当您调用getRepository()
,必须传递实体类,而不是存储库本身,因此将是这样的:
$this->getDoctrine()->getRepository(Checklist::class);
即使您这样做,您的IDE也不知道该方法存在。 您的IDE实际上是错误的,该方法确实存在,您的IDE无法知道getRepository()
调用返回了什么对象。
如何避免呢? 选择以下解决方案之一(它们都可以在PhpStorm中使用,选项1应该在任何地方都可以使用,选项2可能在所有现代IDE中都可以使用,我不知道其他IDE中对选项3的支持):
选项1:将其作为服务注入
public function myControllerRoute(ChecklistRepository $checklistRepository) {
// now your IDE knows what methods are inside the $checklistRepository
$items = $checklistRepository->getAllItemsForChecklist();
}
选项2:将其提示给IDE(和其他开发人员)
public function myControllerRoute() {
/** @var ChecklistRepository $checklistRepository */
$checklistRepository = $this->getDoctrine()->getRepository(Checklist::class);
// after the typehint the IDE knows what type it is
$items = $checklistRepository->getAllItemsForChecklist();
}
选项3:使用断言
public function myControllerRoute() {
$checklistRepository = $this->getDoctrine()->getRepository(Checklist::class);
assert($checklistRepository instanceof ChecklistRepository);
// after the assert the IDE knows what type it is
$items = $checklistRepository->getAllItemsForChecklist();
}
选项2和3几乎相同,但是选项3有一个额外的好处,即如果$checklistRepository
不是ChecklistRepository
实例,则在开发机器上它将抛出异常,在生产环境中assert()
调用将被忽略并且不会减慢执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.