Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention: singular vs plural for classes describing entities in PHP

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")?

like image 497
rfc1484 Avatar asked Oct 27 '25 03:10

rfc1484


2 Answers

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.

like image 82
Ondřej Mirtes Avatar answered Oct 28 '25 17:10

Ondřej Mirtes


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.

like image 32
Nev Stokes Avatar answered Oct 28 '25 17:10

Nev Stokes