drazvzi741287 2010-04-13 14:00
浏览 16
已采纳

Doctrine能否使用generateModelsFromDb检测数据库中的n-m关系?

When using generateModelsFromDb to generate the models Doctrine makes one to many relations between the relation table and the base tables instead of generating a nm-relation between the base tables themselves. Is there any way to let generateModelsFromDb detect the n-m relation?

  • 写回答

1条回答 默认 最新

  • dsj60862 2010-04-14 21:27
    关注

    You can make your design of the DB, with YAML. If you want to make a N-M relation you need to do something like this (it shows a fool example about dogs with more than one owner and owners with more than a dog)

    ---
    options:
        type: INNODB
        collate: utf8_unicode_ci
        charset: utf8
    
    Human:
        columns:
            id:
                type: integer(4)
                primary: true
                autoincrement: true
            name:
                type: varchar(64)
                notnull: true
            ...
            ...
        relations:
            Dogs:
                foreignAlias: Humans
                class: Dog
                ref_class: Human_Dogs
    Dog:
        columns:
            id: 
                type: integer(4)
                autoincrement: true
                primary: true
            owner:
                type: varchar(64)
                notnull: true
            ...
            ...
        relations:
            Humans:
                foreignAlias: Dogs
                class: Human
                ref_class: Human_Dogs
    Human_Dogs:
        columns: 
            human_id:
                type: int(4)
                primary: true
            dog_id: 
                type: int(4)
                primary: true
        relations:
            Human:
                foreignAlias: Human_Dogs
            Dog:
                foreignAlias: Human_Dogs
    

    That is going to be the file.yml, so now you can generate the DB from this file. The way you can do that depends a lot on the framework or program you are working with. Anyway here there is a simple way to do it in PHP + Doctrine:

    <?php 
    $options = array(
          'packagesPrefix'  =>  'Plugin',
          'baseClassName'   =>  'MyDoctrineRecord',
          'suffix'          =>  '.php'
    );
    
    Doctrine_Core::generateModelsFromYaml('/path/to/file.yml', '/path/to/model', $options);
    ?>
    

    With the proper configuration you can autogenerate the id field.

    In this link there is a tutorial -> Documentation

    I hope this helps you!

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

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大