2016-07-21 38 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