使用 Doctrine ORM 的表命名约定 [英] Table naming convention with Doctrine ORM
问题描述
在使用 Doctrine ORM 时是否有命名表的约定?我喜欢用复数命名表格,但如果有约定,我想坚持下去.
Is there a convention for naming tables when using Doctrine ORM? I like to name tables with the plural but if there's a convention I want to stick to it.
因此,表 'users' 将与使用 fk 作为单数 ('user_id') 的表相关.
So the table 'users' would be related to tables using the fk as the singular ('user_id').
对此是否有最佳做法(使用单数或复数表名),如果是后者,这如何适用于复数不是添加s"的简单情况的表.
Is there a best practice for this (using singular or plural table names) and if the latter, how does this apply to tables where the plural isn't a simple case of adding an 's'.
例如,我目前有一个名为 'categorys' 而不是 'categories' 的表来维护添加 's' 的约定.
For example I currently have a table called 'categorys' instead of 'categories' to maintain the convention of adding 's'.
这是一个明智的做法吗?
Is this a sensible approach?
推荐答案
我以前在使用自己的基本 ORM 时使用复数表名,但当我开始使用 symfony + Propel 时我切换到单数表名,现在有点教义.这样做的原因是类名,因为您想创建一个 User 而不是 Users.
I used to use plural table names when using my own basic ORM but I switched over to singular table names when I started using symfony + Propel and now a little bit of Doctrine. The reason for this is for the class names because you want to create a User and not a Users.
使用 Doctrine,当涉及到集合或关系时,你告诉它别名应该是什么:
With Doctrine, when it comes to collections or relations, you tell it what the alias should be:
http://www.学说项目.org/projects/orm/1.2/docs/manual/working-with-models/en
你会看到一个 User 可以有很多 Phonenumber 所以在 YAML 模式中设置了一个 foreignAlias 所以它会是 电话号码通过$user->电话号码有效访问.
You'll see the a User can have many Phonenumber so a foreignAlias was setup in the YAML schema so it would be Phonenumbers effectively accessed via $user->Phonenumbers.
在您的示例中,您将 foreignAlias 设置为 Categories,同时保留名为 Category 的表和记录.
In your example you'll set the foreignAlias to be Categories while keeping the table and record named Category.
这篇关于使用 Doctrine ORM 的表命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!