Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:25:52+00:00 2026-06-01T12:25:52+00:00

In my application I have the following tables: Posts id title content Users id

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:25:54+00:00Added an answer on June 1, 2026 at 12:25 pm

    With you current setup you can do this:

    'contain' => array(
        'TopicPost' => array('Topic')
    )
    

    Or you can add

    public $hasAndBelongsToMany = array(
        'Topic' => array('with' => 'TopicPost')
    );
    

    to the Post model.

    And then your contain will look like this:

    'contain' => array(
        'Topic'
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following requirement in my application. A tableView with games title After selecting
I have following code in my Application. Comments in my code will specify My
I have the following application: I have 1 window. On that window I add
In my Rails 3.2.3 application I have the following code in the application.html.erb to
I have following code in my application: // to set tip - photo in
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following architecture in my application. Client (GWT) <--calls--> Servlet <--calls--> Service <--calls-->
I have following problem: I have built a tabbar application with 4 tabs. I
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have a posts controller and a comments controller. Post has many comments, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.