Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table naming convention with 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.

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?

like image 954
calumbrodie Avatar asked Nov 04 '10 19:11

calumbrodie


People also ask

What is naming convention and example?

A naming convention can include capitalizing an entire word to denote a constant or static variable (which is commonly done in Flash programming), or it could be a simple character limit in a coding language (such as SQL).

How do you name a database convention?

For the traditional naming convention: Database names must only consist of the letters a to z (both lower and upper case allowed), the numbers 0 to 9 , and the underscore ( _ ) or dash ( - ) symbols. This also means that any non-ASCII database names are not allowed. Database names must always start with a letter.

Should DB table names be plural?

When naming tables, you have two options – to use the singular for the table name or to use a plural. My suggestion would be to always go with names in the singular. If you're naming entities that represent real-world facts, you should use nouns. These are tables like employee, customer, city, and country.

What are naming conventions?

What Is a Naming Convention? In simple terms, a naming convention refers to a framework used for naming your files in a specific way. This should be descriptive and consistent throughout the organization. It is always best to use a naming convention to describe the contents of the files.


2 Answers

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.

like image 145
Rob Olmos Avatar answered Sep 20 '22 14:09

Rob Olmos


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.

like image 40
Tom Avatar answered Sep 19 '22 14:09

Tom