Question

Following THIS book I'm trying to do that exercise to associate comments with posts, but I'm not getting it. Here is what I tried:

class Comments extends AppModel {
    var $name = 'Comments';
    var $hasMany = 'Post';
    var $belongsToMany = 'User';

}

class User extends AppModel {
    var $name = 'User';
    var $hasMany = array('Post');



}

class Post extends AppModel {
    var $name = 'Post';
    var $belongsTo = array('User');
    var $hasAndBelongsToMany = array('Tag');
    var $hasMany = array('Comments');



}
Was it helpful?

Solution

I think the problem may lie in your Comments class. I would guess that instead of:

var $hasMany = 'Post';

it should be:

var $belongsTo = 'Post';

And since comments also belong to Users, I would remove the line directly below and instead make it:

var $belongsTo = array('Post', 'User');

OTHER TIPS

I would do this if I were you

supposed this is my comment table, I will store Post_ID everytime I create a comment.

id int,
comment_title text,
comment_body text,
post_id int 

Simply showing comment via post_id might fit your needs,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top