2016-07-21 41 views
3

Symfony的發電機產生的下面的類儲存庫的:如何配置依賴注入庫類的symfony 3

namespace AppBundle\Repository; 
use AppBundle\Entity\GroupEntity; 

/** 
* GroupEntityRepository 
* 
* This class was generated by the Doctrine ORM. Add your own custom 
* repository methods below. 
*/ 
class GroupEntityRepository extends \Doctrine\ORM\EntityRepository 
{ 


} 

services.yml:

group_entity_repository: 
     class: AppBundle\Repository\GroupEntityRepository 
     arguments: ["@doctrine.orm.entity_manager", AppBundle\Entity\GroupEntity] 

我配置錯誤services.yml,但我現在不要用什麼作爲第二個參數。所以我得到的錯誤:

Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, string given, called in E:\other\dropbox\Dropbox\programavimas\kodo pavyzdziai\htdocs\users_admin_demo\var\cache\dev\appDevDebugProjectContainer.php on line 1626 and defined

如何解決它?我在文檔中看不到它,它只顯示了生成器和最終生成的類的代碼,但沒有顯示服務配置。

+0

找到本主題(http://stackoverflow.com/questions/17228417/symfony-2-creating-a-service-from-a -repository),它可能可以幫助你。 –

回答

9

推薦爲Symfony的3.3:

由於Symfony的3.3的,建議使用實際的類名作爲服務ID(read thisthis)。

AppBundle\Repository\GroupEntityRepository: 
    factory: 'Doctrine\ORM\EntityManagerInterface:getRepository' 
    arguments: 
     - AppBundle\Entity\GroupEntity 

原來的答覆:

你可以配置你的資料庫服務是這樣的:

group_entity_repository: 
    class: AppBundle\Repository\GroupEntityRepository 
    factory: ["@doctrine.orm.entity_manager", getRepository] 
    arguments: 
     - AppBundle\Entity\GroupEntity 

你可能永遠不會想自己調用庫的構造。因此,這種方法只是使用entity_manager來獲取存儲庫。 服務容器bascially使用此代碼來獲取庫:

$container->get('doctrine.orm.entity_manager')->getRepository('AppBundle\Entity\GroupEntity'); 
+0

你是上帝。好的,現在應該如何找到這些信息?我的問題是我發現很長的簡單信息。我正在通過此頁面工作:http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes 如果我以您的方式獲得reposotory,我會收到錯誤消息「現有的服務「appbundle \ entity \ groupentity」。「但是當我問$ groupRepo = $ this-> container-> get('group_entity_repository'); - 有用。 –

+0

在這裏您可以找到關於使用工廠定義服務的信息:http://symfony.com/doc/current/components/dependency_injection/factories.html它不是特定於存儲庫,而是應該解釋它的工作原理。 –

+0

另一個很好的信息來源是谷歌搜索欄。不要太過分尖刻,但這個問題已經被問及幾十次(如果不是幾百次)。嘗試複製/粘貼您的標題到谷歌,看看會發生什麼。同樣的搜索技術也可以用於其他一些事情。 – Cerad