【问题标题】:Custom Repository in DoctrineDoctrine 中的自定义存储库
【发布时间】:2018-03-09 11:13:07
【问题描述】:

我有一个控制器:

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);
    }

如何将这一行 (1) 移动到存储库中,然后从控制器中的存储库中使用此方法?

【问题讨论】:

  • 为什么要将它移到存储库中?你想为你的实体创建一个自定义的 findAll 方法吗?
  • @AntoineGalluet 我希望与教义相关的所有内容都在存储库中,并且在控制器中我将使用存储库中的方法
  • 但是即使您在存储库中添加了一个新的 findAll() 函数,您也必须通过 $this-&gt;getDoctrine()-&gt;getRepository(Item::class)-&gt;youCustomFunction() 调用此函数。所以不会有太大变化。

标签: symfony doctrine-orm repository


【解决方案1】:

您突出显示的行实际上与 Doctrine 无关:您从 Dependency Injection 容器获取服务,然后在其上调用方法。

可能困扰您的是您使用的是别名 (getDoctrine()) 和 Doctrine 的注册表,这是为了方便。 但实际上您也可以将存储库声明为服务并执行以下操作:$this-&gt;get('item_repository)->findAll()`。

【讨论】:

    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多