I’ve got a simple CakePHP site (1.2). I’ve got a page where you can edit and save a Person. So I have a Person model and controller.
Each Person has none or more comments, in the comment table. So I have a Comment model, and I have a hasMany association on my Person model to the Comment model. View is working great.
My question is, on the view Person page, I have an add comment button. How should this work? Should I expect the Person controller to include a save for the comment record, or create a comment controller and save it outside of it’s association for a person?
I’m experienced with PHP, but brand new to Cake.
Any ideas? I think I’m just missing something obvious, but I’m not sure what to do. I feel like if this was PHP I would reference the Person_id in my add comment form, and thus use a separate controller, but I feel like having a controller for a simple Model is useless, since Comments are only edited in the context of a Person record.
Ideas?
I’m not a CakePHP expert, but I still think it would make sense to have your own controller. From what I remember from doing one of those CakePHP blog tutorials is, that you need to link the comments and the post in the comment model. This is some of the code I have from it:
And then you need a controller (comments_controller.php):
Some SQL:
The
$scaffoldcreates a CRUD application for you, so when you go to /comments in your browser you can create, read, update and delete comments. So, as you see, there is not much involved here. All you need is your database tables and a little logic to provide person_id.To save a comment (in your Person/view):
And in your CommentsController:
So you basically overwrite the standard add action, which Cake adds by itself. Hope that makes sense now. Also, you might need a route so it picks up /comments/add/ID. I don’t know about this part. 🙂