doushichun9409 2013-11-08 16:09
浏览 48
已采纳

将Doctrine 2 ORM实体存储在不同的位置

Disclaimer: I'm new to Doctrine

I'm currently building a project that requires extreme modularity and flexibility, and have 2 questions related to it. My project considers the following an "ideal" simplified structure:

PackageA
    PackageA.php
    PackageA.orm.yml
PackageB
    PackageB.php
    PackageB.orm.yml
PackageC
    PackageC.php
    PackageC.orm.yml

Because these packages are added or removed fairly frequently, my goal is for each package to define its own mapping for persistence (as shown above). However, the examples of doctrine configuration I've seen, place all the mapping files (xml or yml) into a single directory, in that case my structure would be:

PackageA
    PackageA.php
PackageB
    PackageB.php
PackageC
    PackageC.php
Config
    PackageA.orm.yml
    PackageB.orm.yml
    PackageC.orm.yml

Question 1: Is it possible to keep each mapping configuration file in a different location (like first example), and if so, how do you instantiate an EntityManager with that kind of configuration (I've read the Docs for Setup and can't seem to find this capability).

Question 2: I've seen how to create Schemas using the command line tool, but ideally I would like to each package to create its own schema, so if PackageD is installed onto a system and its schema doesn't exist yet, it is able to create it on the fly. Is that possible with Doctrine?

Note: I've seen some ability for creating schemas on demand with SchemaTool, but it seems limited to create all the schemas, is that true?

  • 写回答

2条回答 默认 最新

  • dre75230 2014-03-24 18:31
    关注

    Answering my own question for those who need this:

    Splitting up the location of all the Maps and Entities is most easily accomplished using the SimplifiedYamlDriver. Here's a quick example:

    <?php
    
    // Generate an array of your structure
    $maps = array(
     'PackageA/Entities' => 'Your\PackageA\Entities',
     'PackageB/Entities' => 'Your\PackageB\Entities'
    );
    
    $config = new Doctrine\ORM\Configuration;
    $driver = new Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver($maps);
    
    $config->setMetadataDriverImpl($driver);
    
    // ... finish configuration
    // ... create connection params $conn
    
    $em = new Doctrine\ORM\EntityManager($conn, $config);
    

    The above code (plus pseudo code) creates a new entity manager with access to Entities in more modular structure. Remember that in your Entity.orm.yml mapping files you should use the fully qualified namespace of the actual Entities.

    More details here: Yaml Mapping

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记