简体   繁体   English

Laravel - 数据库、表和列命名约定?

[英]Laravel - Database, Table and Column Naming Conventions?

I'm using laravel eloquent data objects to access my data, what is the best way to name my tables, columns, foreign/primary keys etc?我正在使用 laravel eloquent 数据对象来访问我的数据,命名表、列、外键/主键等的最佳方法是什么?

I found, there are lots of naming conventions out there.我发现,那里有很多命名约定。 I'm just wondering which one best suits for laravel eloquent models.我只是想知道哪一种最适合 laravel eloquent 模型。

I'm thinking of following naming convention:我正在考虑以下命名约定:

  1. Singular table names (ex: Post)单一表名(例如:Post)
  2. Singular column names (ex: userId - user id in the post table)单列名称(例如:userId - post 表中的用户 ID)
  3. Camel casing for multiple words in table names (ex: PostComment, PostReview, PostPhoto)表名中多个单词的驼峰式大小写(例如:PostComment、PostReview、PostPhoto)
  4. Camel casing for multiple words in column names (ex: firstName, postCategoryId, postPhotoId)列名中多个单词的驼峰式大小写(例如:firstName、postCategoryId、postPhotoId)

So with this, I could use similar syntax in the controller.所以有了这个,我可以在控制器中使用类似的语法。

$result = Post::where('postCategoryId', '4')->get();

Are there any recommended Laravel guidelines for this?是否有任何推荐的 Laravel 指南? Can I proceed with these naming conventions?我可以继续使用这些命名约定吗?

If someone has better suggestions, I will be very happy to hear them.Thanks a lot!如果有人有更好的建议,我会很高兴听到他们的。非常感谢!

Laravel has it's own naming convention. Laravel 有它自己的命名约定。 For example, if your model name is User.php then Laravel expects class 'User' to be inside that file.例如,如果您的模型名称是User.php那么 Laravel 期望类 'User' 位于该文件中。 It also expects users table for User model.该公司还预计usersUser模型。 However, you can override this convention by defining a table property on your model like,但是,您可以通过在模型上定义表属性来覆盖此约定,例如,

    class User extends Eloquent implements UserInterface, RemindableInterface {
        protected $table = 'user';
    }

From Laravel official documentation:来自 Laravel 官方文档:

Note that we did not tell Eloquent which table to use for our User model.请注意,我们没有告诉 Eloquent 将哪个表用于我们的 User 模型。 The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified.除非明确指定另一个名称,否则类的小写复数名称将用作表名。 So, in this case, Eloquent will assume the User model stores records in the users table.因此,在这种情况下,Eloquent 将假设 User 模型将记录存储在 users 表中。 You may specify a custom table by defining a $table property on your model您可以通过在模型上定义$table属性来指定自定义表

If you will use user table id in another table as a foreign key then, it should be snake-case like user_id so that it can be used automatically in case of relation.如果您将在另一个表中使用 user table id 作为外键,那么它应该像user_id这样的蛇形大小写,以便在关系的情况下可以自动使用。 Again, you can override this convention by specifying additional arguments in relationship function.同样,您可以通过在关系函数中指定附加参数来覆盖此约定。 For example,例如,

    class User extends Eloquent implements UserInterface, RemindableInterface {
        public function post(){
            return $this->hasMany('Post', 'userId', 'id');
        }
    }

    class Post extends Eloquent{
        public function user(){
            return $this->belongsTo('User', 'userId', 'id');
        }   
    }

Docs for Laravel eloquent relationship Laravel 雄辩关系文档

For other columns in table, you can name them as you like.对于表中的其他列,您可以随意命名它们。

I suggest you to go through documentation once.我建议您查看一次文档。

I don't agree in general with these examples you both have shown right on here.我总体上不同意你们俩在这里展示的这些例子。

It is clean if you take a look at the official Laravel documentation, especially in the Eloquent's relationship session ( http://laravel.com/docs/4.2/eloquent#relationships ).如果您查看 Laravel 的官方文档,就会很清楚,尤其是在 Eloquent 的关系会话中 ( http://laravel.com/docs/4.2/eloquent#relationships )。

Table names should be in plural, ie 'users' table for User model.表名应该是复数,即用户模型的'users'表。

And column names don't need to be in Camel Case, but Snake Case.并且列名不需要在 Camel Case 中,但需要在 Snake Case 中。 See it is already answered: Database/model field-name convention in Laravel?看到已经回答了: Laravel 中的数据库/模型字段名称约定?

It is too usual you can see it is like the RedBeanORM: the Snake Case for columns, even if you try other one.太常见了,您可以看到它就像 RedBeanORM:列的 Snake Case,即使您尝试其他列也是如此。 And it is adviced to avoid repeating the table names with column ones due to the method you can call from the Model object to access their relationships.由于可以从 Model 对象调用以访问它们的关系,因此建议避免将表名与列名重复。

    class User extends Eloquent implements UserInterface, RemindableInterface {
        public function post(){
            return $this->hasMany('Post');
        }
    }

    class Post extends Eloquent{
        public function user(){
            return $this->belongsTo('User');
        }   
    }

The default table naming conventions can easily cause conflicts with the installation of multiple packages who may have incidentally the same class names.默认的表命名约定很容易与多个包的安装发生冲突,这些包可能偶然具有相同的类名。 A solution would be to name tables as: [vendor].[package].[class], which is in line with how namespacing in Laravel is applied.一种解决方案是将表命名为:[vendor].[package].[class],这与 Laravel 中命名空间的应用方式一致。

Edited : Using dots in table names is not recommended though.编辑:虽然不建议在表名中使用点。 Would there be an alternative convention to use to ensure developers of a modular built application do not need to worry about existing table names.是否有替代约定可用于确保模块化构建应用程序的开发人员无需担心现有表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM