【问题标题】:Symfony 3: can't overwrite repositorySymfony 3:无法覆盖存储库
【发布时间】:2016-05-17 13:09:41
【问题描述】:

我从 Symfony 2.7升级到 3.0。它几乎可以工作。只有问题:我无法在下面的代码中覆盖AppBundle\Entity\MyRepository,甚至不需要该文件(我将其重命名以进行测试)并被 Symfony 跳过.

app/config/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\Entity\MyRepository

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

AppBundle\Controller\MyController.php

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

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

提前致谢!

【问题讨论】:

  • 您检查过$this->get('myRepositoryService') 是否是AppBundle\Entity\MyRepository 的实例吗?您是否在实体映射中将repositoryClass 设置为此类?
  • @dragoste 我忘了为这个实体设置repositoryClassfacepalm 请将其发布为答案,我会接受。非常感谢!
  • 我也忘记了几次。 ;-)

标签: php dependency-injection symfony


【解决方案1】:

以下代码在 Symfony 3.0 中为我工作:

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

请注意与您的工厂服务名称不同,并且工厂方法和实体名称没有单引号。

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

【讨论】:

  • 最好用冒号引用字符串 - arguments: [ 'AppBundle:SomeEntity' ]
猜你喜欢
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 2017-07-05
  • 2013-02-17
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多