为什么 CakePHP 使用不同的复数/单数命名约定?

发布于 2024-08-11 17:45:43 字数 235 浏览 5 评论 0原文

有人可以在这里解释一下为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小伙你站住 2024-08-18 17:45:43

CakePHP 约定

CakePHP 的约定是从多年的 Web 开发经验和最佳实践中提炼出来的。虽然我们建议您在使用 CakePHP 进行开发时使用这些约定,但我们应该指出,其中许多原则很容易被覆盖 - 这在使用遗留系统时特别方便。

我认为这个想法是为了让阅读和以正确的方式思考元素变得更加流畅。数据库表总是复数,因为它们保存许多记录。该模型是单一的,因为您应该考虑用它查找单个记录。 model_id 的选择字段将自动从 $models 获取其选项,因为您选择了多个选项之一。

$model = $this->Model->find('first');  // reads like English
$model = $this->Models->find('first'); // slightly trips you up

$models = $this->Model->find('all');   // works okay
$models = $this->Models->find('all');  // more logical, but "this models" still trips

它并不总是完美的,但我认为一旦你习惯了它,它就会是一个很好的约定。不过,我知道一开始它会让人感到困惑。

CakePHP Conventions

CakePHP’s conventions have been distilled out of years of web development experience and best practices. While we suggest you use these conventions while developing with CakePHP, we should mention that many of these tenets are easily overridden – something that is especially handy when working with legacy systems.

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.

$model = $this->Model->find('first');  // reads like English
$model = $this->Models->find('first'); // slightly trips you up

$models = $this->Model->find('all');   // works okay
$models = $this->Models->find('all');  // more logical, but "this models" still trips

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.

花落人断肠 2024-08-18 17:45:43

有没有简单的方法可以记住?

是的,对于命名约定重要的部分(db/model/controller)...如果它不是模型的名称(即User)或外键的名称(即. user_id),那么它是复数。除了这两件事之外,一切基本上都是复数。

is there an easy way to remember?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文