4

I think that the standard practice to name tables in MySQL is to use plural names.

The classes refering to those tables should also be plural?

For example, imagine that you have a table called Users, that is used for authentication purposes.

This table would be described in an entity class more or less like this using the doctrine ORM:

namespace Company\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="Users")
 */
class Users
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="user_id")
     * @ORM\GeneratedValue(strategy="AUTO")
     * 
     * @var integer $userId
     */
    protected $userId;

    /**
     * @ORM\Column(type="string", length="255", name="first_name")
     * 
     * @var string $userName
     */
    protected $userName;
    ...
}

Is this correct?

Or should the class "Users" be named in singular ("User")?

rfc1484
  • 9,441
  • 16
  • 72
  • 123
  • I (and I think most people) would use singular because it represents a single user. I also use singular names for tables because I name them after what a single row represents, but I'm probably in the minority on that one. – Matthew Jun 05 '11 at 17:52

4 Answers4

5

If your class is supposed to represent an instance of a real-world item then semantically I'd say that singular form may be the one to go with. The database table is used to store multiple items so plural form is appropriate there. Either way it doesn't really matter just as long as you're consistent.

Nev Stokes
  • 9,051
  • 5
  • 42
  • 44
5

Class representing one database row (an entity) should have singular name. Doctrine 2 default behaviour is to name database tables the same way. You can reconfigure it in every @Table annotation if you'd like to, but I suggest you to stick with Doctrine naming conventions - singular name for database table is also acceptable.

Ondřej Mirtes
  • 5,054
  • 25
  • 36
3

In the database, they're plural because it's a table of a lot of them; a table of lots of users. As an object, it's a singular thing; a single user. I generally keep my classes singular.

Bob Baddeley
  • 2,264
  • 1
  • 16
  • 22
0

I also subscribe to using table names with singular name because a row represents one item of a table in the same way as an object represents one instance of class. The only exceptions can be when each row of a table contains for example serialized (or equivalent) items, i.e. "neighbors" of a location.

Jorj
  • 2,495
  • 1
  • 19
  • 12