Table naming convention with Doctrine ORM

谁说我不能喝 提交于 2020-01-02 01:53:06

问题


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.

So the table 'users' would be related to tables using the fk as the singular ('user_id').

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

For example I currently have a table called 'categorys' instead of 'categories' to maintain the convention of adding 's'.

Is this a sensible approach?


回答1:


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.

With Doctrine, when it comes to collections or relations, you tell it what the alias should be:

http://www.doctrine-project.org/projects/orm/1.2/docs/manual/working-with-models/en

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.

In your example you'll set the foreignAlias to be Categories while keeping the table and record named Category.




回答2:


The Doctrine convention is to use singular names for tables and models, as the first answerer explains, because logically you want methods like:

$user->Phonenumbers

... instead of:

$user->Phonenumberss

The definitions can be customised through Aliases.



来源:https://stackoverflow.com/questions/4100489/table-naming-convention-with-doctrine-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!