In my application I have the following tables:
Posts
id
title
content
Users
id
username
password
Profiles
id
user_id
firstname
lastname
Comments
id
post_id
user_id
content
Topics
id
title
Topic_Posts
id
topic_id
post_id
Hopefully the relationships are fairly obvious! The problem I am having is that last table is a join table for the many to many relationship between Posts and Topics. I'm not entirely sure how to set this up!
Here is my Models for Posts, Topics and Topics_Posts:
class Post extends AppModel
{
public $name = 'Post';
public $belongsTo = 'User';
public $hasMany = array('Comment', 'TopicPost');
public $actsAs = array('Containable');
}
class Topic extends AppModel
{
public $hasMany = array(
'TopicPost'
);
}
class TopicPost extends AppModel {
public $belongsTo = array(
'Topic', 'Post'
);
}
As far as I can tell the relationships are correct? And then in my method I have the following statement:
$post = $this->Post->find('first',
array('contain'=>
array(
'Comment'=>array('User'=>array('Profile')),
'User'=>array('Comment','Profile')
),
'conditions'=>array('Post.id'=>$id)));
So basically my question is how to add the Topic to the contain as I haven't had to add the Comment and User to the Post model so not sure why or what to do next...
Also how would I pull the Topics out again? As I'm confused as to how this will work considering I'm linking to the join table rather than the ACTUAL Topics... Unless I'm just way off track. Help on this would be appreciated.