通过services.yml将自定义实体存储库类传递给服务

时间:2012-02-01 17:43:51

标签: dependency-injection symfony doctrine-orm

我尝试通过services.yml

加载我的服务和自定义存储库

现在我有:

parameters:
    job_entity_name:  Marek\JobeetBundle\Entity\Job
    category_entity_name:  Marek\JobeetBundle\Entity\Category

services:
  job_metadata:
    class: Doctrine\ORM\Mapping\ClassMetaData
    arguments: 
      name: [%job_entity_name%]
  category_metadata:
    class: Doctrine\ORM\Mapping\ClassMetaData
    arguments: 
      name: [%category_entity_name%]
  job_repo:
    class: Marek\JobeetBundle\Repositories\JobRepository
    arguments: 
      em: [@doctrine.orm.entity_manager]
      class: [@job_metadata] 
  category_repo:
    class: Marek\JobeetBundle\Repositories\CategoryRepository
    arguments: 
      em: [@doctrine.orm.entity_manager]
      class: [@category_metadata]      
  job_service:
    class: Marek\JobeetBundle\JobService
    arguments:    
      jobRepo: [@job_repo]
      categoryRepo: [@category_repo]

错误:

  

注意:数组转换为字符串   d:\杰韦利\ Symfony2的\ Jobeet的\厂商\教义\ lib中\学说\ ORM \映射\ ClassMetadata.php   第66行

我放弃了。有人可以帮忙吗?

[编辑]

当然我的架构很糟糕,正确的是:

services:
  job_metadata:
    class: Doctrine\ORM\Mapping\ClassMetaData
    arguments: 
      name: "Marek\JobeetBundle\Entity\Job"
  category_metadata:
    class: Doctrine\ORM\Mapping\ClassMetaData
    arguments: 
      name: "Marek\JobeetBundle\Entity\Category"
  job_repo:
    class: Marek\JobeetBundle\Repositories\JobRepository
    arguments: 
      em: @doctrine.orm.entity_manager
      class: @job_metadata 
  category_repo:
    class: Marek\JobeetBundle\Repositories\CategoryRepository
    arguments: 
      em: @doctrine.orm.entity_manager
      class: @category_metadata      
  job_service:
    class: Marek\JobeetBundle\JobService
    arguments:    
      jobRepo: @job_repo
      categoryRepo: @category_repo

1 个答案:

答案 0 :(得分:3)

在这个答案中,您将找到一种更好的方法将存储库定义为服务(即使用工厂服务和工厂方法)。

Injecting repositories