good_evening
good_evening

Reputation: 21749

Blog with CakePHP

I have Posts_controller and I have Comments_controller and their models and views.

Everything is alright, but my question, how can I combine them, so comments would appear in the same page as posts? In CakePHP tutorial they don't do that. I could include, but I don't think it's appropriate. Thanks.

Upvotes: 1

Views: 157

Answers (2)

dubvfan87
dubvfan87

Reputation: 641

Here is the short answer to your question:

You need to do a hasMany relation like this: Posts -> HasMany -> Comments

You set that up in your Posts model file. models/Post.php

Then when you run a $this->Post->find(..) - It will return comments for the post. You then use a foreach in the helper to print them out.

foreach($post['Comment'] as $comment) {
...
}

Upvotes: 4

kaklon
kaklon

Reputation: 2432

There is not short answer to your question. You should read cake manual, model associations, and especially HasMany association. If you do the models relation first and then bake your controllers and views, you'll have the comments on the same page as your posts.

Upvotes: 3

Related Questions