簡體   English   中英

Doctrine2+Symfony2:如何在 Symfony2 中使用命名空間的 Doctrine 實體?

[英]Doctrine2+Symfony2: How to use namespaced Doctrine entities with Symfony2?

我嘗試在 Symfony 2.5 中使用 Doctrine 和實體映射的 XML 配置。

我有一個命名空間類BarACME\\TestBundle\\Entity\\Foo\\Bar

由於我有很多實體,它們不能都駐留在ACME\\TestBundle\\Entity命名空間中,但必須放入子命名空間中。

創建實體沒問題,但我不知道將 ORM XML 配置文件放在哪里。

我試過Resources/config/doctrine/Foo/Bar.orm.xml ,它沒有找到映射文件:

$ php app/console doctrine:schema:create --dump-sql
No Metadata Classes to process.

我嘗試了Resources/config/doctrine/Bar.orm.xml ,它忽略了Entity的額外Foo命名空間,盡管完整的命名空間在name元素的Bar.orm.xml中正確給出。

$ php app/console doctrine:schema:create --dump-sql                  
[Doctrine\Common\Persistence\Mapping\MappingException] 
Class 'ACME\TestBundle\Entity\Bar' does not exist  

我錯過了什么? XML 映射文件駐留在這些命名空間類的正確位置是什么?

使用@user3749178 的 Foo.Bar.orm.xml 建議有效,並且是解決問題的最簡單方法,盡管所有映射文件最終都在一個目錄中。

也可以根據以下內容為所有內容設置單獨的目錄:

http://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration

這是一個示例配置:

doctrine:

  orm:
    default_entity_manager:       default
    auto_generate_proxy_classes: %kernel.debug%

    entity_managers:

    default:
      connection: default
      mappings:
        foo1: 
          prefix: Cerad\Bundle\ProjectGameBundle\Doctrine\Entity\Foo1
          type:   yml
          dir:    src/ProjectGameBundle/Doctrine/EntityMapping/Foo1
          is_bundle: false
        foo2: 
          prefix: Cerad\Bundle\ProjectGameBundle\Doctrine\Entity\Foo2
          type:   yml
          dir:    src/ProjectGameBundle/Doctrine/EntityMapping/Foo2
          is_bundle: false

您基本上為每個包含實體的目錄指定一個映射。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM