Table naming convention with Doctrine ORM

2019-04-26 05:29发布

问题:

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.