为什么 CakePHP 使用不同的复数/单数命名约定?
有人可以在这里解释一下为什么 CakePHP 有一个约定,即对数据库表和控制器使用复数名称,对模型使用单数名称?为什么不总是使用单数术语,或者总是使用复数?
对我来说,总是不得不思考“现在我在这里使用复数还是单数?”似乎很令人困惑。 (或者有一种简单的方法可以记住吗?)然后您就拥有了使用两者组合的连接表!
我认为某处有一个很好的理由,但只是还没有遇到它(我真的希望这不仅仅是因为 Ruby-on-Rails 就是这样工作的)。
Can somebody perhaps explain here why on earth CakePHP has a convention of using plural names for db tables and controllers and singular for models? Why not always use singular terms, or always plural?
For me it seems confusing to always have to think "now do I use plural or singular here?" (Or is there an easy way to remember?) And then you have the join-tables that use a combination of both!
I assume there's a good reason somewhere, but just have not come across it (I really hope it's not just because Ruby-on-Rails works that way).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这个想法是为了让阅读和以正确的方式思考元素变得更加流畅。数据库表总是复数,因为它们保存许多记录。该模型是单一的,因为您应该考虑用它查找单个记录。
model_id
的选择字段将自动从$models
获取其选项,因为您选择了多个选项之一。它并不总是完美的,但我认为一旦你习惯了它,它就会是一个很好的约定。不过,我知道一开始它会让人感到困惑。
I think the idea is to make it more fluent to read and to think of elements in the right way. Database tables are always plural, because they hold many records. The model is singular, because you should think about finding a single record with it. A select field for
model_id
will automatically get its options from$models
, because you select one of many.It's not always perfect, but I think it's quite a nice convention once you get used to it. I can see how it can be confusing in the beginning though.
是的,对于命名约定重要的部分(db/model/controller)...如果它不是模型的名称(即
User
)或外键的名称(即.user_id
),那么它是复数。除了这两件事之外,一切基本上都是复数。Yes, with respect to the parts where naming convention counts (db/model/controller)... If it's not the name of a model (ie.
User
) or the name of a foreign key (ie.user_id
), then it is plural. Everything is basically plural apart from those two things.