CakePHP 命名约定/文件结构
我对 CakePHP 很陌生,所以我想知道是否有人可以帮助我如何订购我的页面。
我有一个产品表(带有产品模型和产品控制器)。 我还有一个类别表(带有类别模型和类别控制器)。 类别有很多产品。
首先,类别这个名字是不是不正确。根据 CakePHP 约定,正确的名称是什么?
其次,我希望用户单击产品链接,然后显示一个类别列表,最后,一旦他/她选择了一个类别,就会显示该类别中的产品。这将如何安排?
I'm quite new to CakePHP so I'm wondering if anyone can help me with how to order my pages.
I have a table of products (with a Product model and products_controller).
I also have a table of categories (with a Category model and categories_controller).
The categories hasMany products.
Firstly, is the name categories incorrect to call it. According to CakePHP convention, what is the correct name to call it?
Secondly I would like the user to click on the products link and then be presented with a list of categories and finally, once he/she chooses a category be presented with the products in that category. How would this be laid out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你问的是一些非常基本的 CakePHP 的东西,我建议你阅读书,它概述了命名约定、文件结构以及数据检索等等。
话虽这么说,名称类别是正确的,除非您希望产品有多个类别,否则关系将为产品“属于”类别。
要获取产品控制器内的类别信息,您只需使用
$this->Product->Category->find();
访问它的查找方法,但我再次建议您通读 CakePHP当您去建立我们的知识并了解有关您正在使用的框架的更多信息时,请阅读本书。You're asking some pretty basic CakePHP stuff, I suggest you read the book, which outlines naming conventions, file structure and data retrieval to name a few things.
That being said, the name categories is correct, unless you want products to have more than one category, the relationship will be Product 'BelongsTo' Category.
To get category info inside the product controller you can just access it's find methods with
$this->Product->Category->find();
, but again I recommend you read through the CakePHP book as you go to build up our knowledge and learn more about the framework you're using.你的意思是categories不是category的复数形式?我想是的。您的表必须命名为“类别”。
其次,我认为您的模型中需要一个类别 hasAndBelongsToMany Products (HABTM),因此每个类别都有许多产品,并且一个类别属于许多产品。
使用“蛋糕烘烤”命令,您将很容易看到它是否是您想要的。
希望它有帮助,尽管我对 cakePHP 也很陌生......
Alf。
You mean that categories is not a plural of category? I think so. Your table has to be named as 'categories'.
Secondly, I think that you need a Categories hasAndBelongsToMany Products (HABTM) in your model, so every Category has many Products, and also a Category belongs to many products.
Use the 'cake bake' command and you will see easily if it is what you want.
Hope it helped, althought I'm quite new in cakePHP as well...
Alf.
如果数据库中有类别表,其控制器将为categories_controller.php,并且如果产品仅属于一个类别,则产品属于类别将起作用。无需 HABTM 关系。请参阅 cakephp,模型文件采用单数形式,控制器文件采用复数形式,并附有控制器。表在 db 中以复数形式命名。
关于你的第二个问题,我想我没有完全明白。
If you have categories tables in db, its controller would be categories_controller.php and the Products belongsTo Category will work if products belong to only one category. No need to HABTM relationship. See in cakephp the model files are in singular form and controller file are in plural form with controller attached with them. The tables are named in plural in db.
Regarding ur 2nd question, I think im not getting it exactly.