简体   繁体   English

教义1.2多对多关系的列命名约定

[英]Doctrine 1.2 Column Naming Conventions for Many To Many Relationships

I'm working with an existing database schema, and trying to setup two Doctrine models with a Many to Many relationship, as described in this document 我正在使用现有的数据库架构,并尝试设置两个具有多对多关系的Doctrine模型, 如本文档所述

When creating tables from scratch, I have no trouble getting this working. 从头开始创建表时,我毫不费力地开始工作。 However, the existing join tables use a different naming convention that what's described in the Doctrine document. 但是,现有的联接表使用与Doctrine文档中所述不同的命名约定。 Specifically 特别

Table 1
--------------------------------------------------
table_1_id
....other columns....

Table 2
--------------------------------------------------
table_2_id
....other columns....

Join Table
--------------------------------------------------
fktable1_id
fktable_2_id

Basically, the previous developers prefaced all foreign keys with an fk . 基本上,以前的开发人员在所有外键之前都加上了fk

From the examples I've seen and some brief experimenting with code, it appears that Doctrine 1.2 requires that the join table use the same column names as the tables it's joining in 从我所看到的示例和一些简短的代码实验中,似乎Doctrine 1.2 要求联接表使用与其连接的表相同的列名。

  1. Is my assumption correct? 我的假设是否正确?

  2. If so, has the situation changed in Doctrine 2? 如果是这样,那么《学说2》中的情况是否有所改变?

  3. If the answers to either of the above are true, how do you configure the models so that all the columns "line up" 如果以上任何一个的答案都是正确的,那么如何配置模型,使所有列“对齐”

You just have to set the local and foreign parts to match your column names 您只需要设置本地和外部部分以匹配您的列名

Table1:
  columns:
    table_1_id: int
    ....
  relations:
    Table2:
      foreignAlias: FromSecond
      local: table_1_id
      foreign: fktable1_id
      refClass: JoinTable
Table2:
  columns:
    table_2_id: int
    ....
  relations:
    Table1:
      foreignAlias: FromFirst
      local: table_2_id
      foreign: fktable2_id
      refClass: JoinTable
JoinTable:
  columns:
    fktable1_id: int
    fktable2_id: int
  relations:
    Table1:
    Table2:

Take a look at the docs regarding N:M relations. 看看有关N:M关系的文档

When you look at 当你看着

$this->hasColumn('user_id', 'integer', null, array(
                'primary' => true
            )
        );

from the docs, you see that is is up to you how you name the column in your join model. 从文档中,您将看到如何确定联接模型中的列的方式取决于您。

Since you have an existing database, why not generate the models via Doctrine ? 由于您已有数据库,为什么不通过Doctrine生成模型 The API shows you the needed options for this. API向您显示所需的选项。 I used that myself and it works pretty good. 我自己用过,效果很好。 Sometimes you may need some cleanups if you have a compliacted structure but the generated files are a very good start. 如果您的结构比较复杂,有时可能需要进行一些清理,但是生成的文件是一个很好的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM