2016-05-17 62 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