Symfony 条令中的自定义存储库

Symfony 条令中的自定义存储库,symfony,doctrine-orm,repository,Symfony,Doctrine Orm,Repository,我有一个控制器: public function getAllItemsAction() { $content = $this->getDoctrine()->getRepository(Item::class)->findAll();//<-(1)--THIS TO REPOSITORY if ($content === NULL) { return new View("Items not found",

我有一个控制器:

public function getAllItemsAction()
    {
        $content = $this->getDoctrine()->getRepository(Item::class)->findAll();//<-(1)--THIS TO REPOSITORY
        if ($content === NULL) {

            return new View("Items not found", Response::HTTP_NOT_FOUND);
        }

        return new View($content,Response::HTTP_OK);
    }
公共函数getAllItemsAction()
{

$content=$this->getDoctrine()->getRepository(Item::class)->findAll();//您突出显示的行实际上与Doctrine无关:您从依赖项注入容器获取服务,然后对其调用方法

可能会困扰您的是,您正在使用别名(
getDoctrine()
)和来自Doctrine的注册表,这是为了方便起见。
但实际上,您也可以将存储库声明为服务并执行以下操作:
$this->get('item\u repository
)->findAll()`.

为什么要将其移动到存储库中?是否要为您的实体创建自定义findAll方法?@AntoineGalluet我希望与条令相关的所有内容都在存储库中,在控制器中,我将使用存储库中的方法,但即使您添加了新的findAll()函数,您必须通过
$this->getdoctor()->getRepository(Item::class)->youCustomFunction()
调用此函数,所以不会有太大的变化。