I’m new to CakePHP and I trying out some of the tutorials on it.
Currently I have a Categories table and an Items table; Category hasMany Item and Item belongsTo a Category.
On my Items table, I have a category_id column that refers to a category ID in the Categories table.
A user is only limited to adding an item through the categories view.ctp page and it’ll redirect user to the items add.ctp page. The add() function resides in the items controller.
How do I get the ID of the category which the user wish to add an item to and insert them to category_id column whenever a user adds an item?
Thanks in advance.
In the controller:
The view:
(which is equivalent to:)
You just need to get a list of categories, which
find('list')is specialized for, and aninput element which lets the user select a category. The
category_idwill be saved like any other field of theItemmodel.If you want to prefill the category from a different page, use something like this:
View:
From your category selection list, just link to the Item add action: