2016-05-17 60 views
2

升级从Symfony 2.7到3.0。它几乎工作。只有问题:我不能在下面的代码中覆盖AppBundle\Entity\MyRepository,该文件甚至没有必要(我将其重命名为测试)并被Symfony跳过。Symfony 3:无法覆盖存储库

应用/配置/ services.yml

# old, 2.7 (worked): 
myRepositoryService: 
    class: AppBundle\Entity\MyRepository 
    factory_service: doctrine.orm.entity_manager 
    factory_method: getRepository 
    arguments: [AppBundle\Entity\MyEntity] 

# new, 3.0 (skips the MyRepository): 
myRepositoryService: 
    class: AppBundle\Entity\MyRepository 
    factory: ['@doctrine.orm.entity_manager', 'getRepository'] 
    arguments: ['AppBundle\Entity\MyEntity'] 

的appbundle \实体\ MyRepository

class MyRepository extends EntityRepository { 
    public function findAll() { 
     die("MyRepository->findAll()"); // not executed 
    } 
} 

的appbundle \控制器\ MyController.php

$myItems = $this->get('myRepositoryService')->findAll(); // should die but doesn't 

我是否错误地配置了我的services.yml以便Symfony为该实体创建临时存储库文件,而不是使用我创建的文件?

在此先感谢!

+0

您是否检查过'$ this-> get('myRepositoryService')'是否是AppBundle \ Entity \ MyRepository'的一个实例?你有没有在实体映射中将'repositoryClass'设置为这个类? –

+0

@dragoste我忘了为这个实体设置'repositoryClass'。 * facepalm *请将它作为答案发布,我会接受它。非常感谢! –

+1

我也忘了那几次。 ;-) –

回答

2

下面的代码是为我工作中的Symfony 3.0:

some_repository: 
    class: AppBundle\Repository\SomeRepository 
    factory: ['@doctrine.orm.default_entity_manager', getRepository] 
    arguments: [AppBundle:SomeEntity] 

请注意比你的工厂服务名称不同,不存在单引号工厂方法和实体名称。

另外正如我在我的评论中提到的,应该在实体的映射中设置repositoryClass

+0

最好用冒号引用字符串''参数:['AppBundle:SomeEntity']' – luchaninov