2
votes

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?

3

3 Answers

2
votes

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.

1
votes

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.

0
votes

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.