dpvmjk0479 2011-11-17 11:56
浏览 70
已采纳

Doctrine2 + Symfony2:如何将实体和映射存储在单独的包中?

I want to decouple my application so I can use my entities anywhere whatever their datasource might be.

So I've put my entities in

DataAccessLayerBundle\Entity

and mappings in

DataProvider\DataBaseBundle\Resources\config\doctrine

Issue:

When I try the following command:

php app\console doctrine:schema:create

to build my database, I get the following error:

Warning: class_parents(): Class (..)\DataProvider\DatabaseBundle\Entity\BaseEntity does not exist and could not be loaded in (...)\vendor\doctrine\lib\D octrine\ORM\Mapping\ClassMetadataFactory.php line 223

The question is: why the hell is it trying to look for classes in the DataProvider\DatabaseBundle\Entity namespace, when I explicitly defined it otherwise in the mapping files? Example fragment of mapping file:

<mapped-superclass name="(...)\DataAccessLayerBundle\Entity\Base\BaseEntity">

I've noticed symfony/doctrine is just looking up those classes based off xml filenames (e.g. BaseEntity.orm.xml) and current bundle namespace.

Is there any way I can decouple things like I wanted, or am I stuck with defining entities and their mapping in the same bundle?

  • 写回答

1条回答 默认 最新

  • douju1365 2011-11-23 11:32
    关注

    As it turned out from the docs, it is possible to have entities and mappings in separate bundles. To do so, you have to configure doctrine in the main config as in following example:

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        ...
        entity_managers:
          default:
            mappings:
              (...)DataAccessLayerBundle:
                type: xml
                dir: ../DataProvider/DatabaseBundle/Resources/config/doctrine
    

    I still had to move my BaseEntity class from Entity\Base to Entity folder for this to work, but this is a little sacrifice compared to the flexibility gained.

    Config reference: http://symfony.com/doc/2.0/reference/configuration/doctrine.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀