命名约定:在PHP中描述实体的类的单数与复数 [英] Naming convention: singular vs plural for classes describing entities in PHP

查看:174
本文介绍了命名约定:在PHP中描述实体的类的单数与复数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为在MySQL中命名表的标准做法是使用复数名称。



引用这些表的类也应该是复数形式。



例如,假设您有一个名为Users的表,用于认证目的。



此表将被描述在实体类中或多或少这样使用教义ORM:

 命名空间Company\BlogBu​​ndle\Entity; 

使用Doctrine\ORM\Mapping作为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;
...
}

这是否正确?



或者类用户是否以单数(用户)命名?

解决方案

表示一个数据库行(一个实体)的类应该具有单数名称。 Doctrine 2的默认行为是以相同的方式命名数据库表。您可以在每个@Table注释中重新配置它,如果您愿意,但我建议您坚持使用Doctrine命名约定 - 数据库表的单数名称也是可以接受的。


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

解决方案

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.

这篇关于命名约定:在PHP中描述实体的类的单数与复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆