为什么CakePHP使用不同的复数/单数命名约定? [英] Why does CakePHP use different plural/singular naming conventions?

查看:210
本文介绍了为什么CakePHP使用不同的复数/单数命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可能解释这里为什么CakePHP有一个约定使用复数名称为数据库表和控制器和奇异的模型?为什么不总是使用单数术语,或总是复数?对我来说,似乎很困惑,总是要想现在我在这里使用复数还是单数? (或者有一个简单的方法记住??)然后你有连接表,使用两者的组合!

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!

我假设有一个很好的理由,但只是没有遇到它。

(我真的希望它不只是因为Ruby-on-Rails工作方式。)

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.)

西蒙。

推荐答案


CakePHP约定

CakePHP的约定已经从多年的网络发展经验和最佳做法。虽然我们建议您在使用CakePHP开发时使用这些约定,但我们应该提到,很多这些信条很容易被覆盖 - 这在使用旧系统时特别方便。

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.

我认为这个想法是让它更流畅地阅读和以正确的方式思考元素。数据库表总是复数,因为它们拥有许多记录。模型是单数,因为你应该考虑找到一个单一的记录。 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.

$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.

这篇关于为什么CakePHP使用不同的复数/单数命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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