Doctrine 是否能够使用generateModelsFromDb 检测数据库中的nm 关系?

发布于 2024-08-28 12:20:03 字数 111 浏览 5 评论 0原文

当使用generateModelsFromDb生成模型时,Doctrine在关系表和基表之间建立一对多关系,而不是在基表本身之间生成nm关系。有没有办法让generateModelsFromDb检测nm关系?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滴情不沾 2024-09-04 12:20:03

您可以使用 YAML 进行数据库设计。如果你想建立一个 NM 关系,你需要做这样的事情(它显示了一个关于拥有多个主人的狗和拥有不止一只狗的主人的愚蠢示例)

---
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

这将是 file.yml,所以现在你可以从该文件生成数据库。您可以执行此操作的方式在很大程度上取决于您正在使用的框架或程序。无论如何,这里有一个简单的方法可以在 PHP + Doctrine 中做到这一点:

<?php 
$options = array(
      'packagesPrefix'  =>  'Plugin',
      'baseClassName'   =>  'MyDoctrineRecord',
      'suffix'          =>  '.php'
);

Doctrine_Core::generateModelsFromYaml('/path/to/file.yml', '/path/to/model', $options);
?>

通过正确的配置,您可以自动生成 id 字段。

在此链接中有一个教程 -> 文档

我希望这对您有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文