Why does CakePHP use different plural/singular naming conventions?

六眼飞鱼酱① 提交于 2019-12-04 06:59:13

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!