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 7815475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:32:04+00:00 2026-06-02T05:32:04+00:00

I have an application that stores Posts and Topics and joins them using a

  • 0

I have an application that stores Posts and Topics and joins them using a Topic_Posts table.

The associations for the application are as follows:

Post.php
class Post extends AppModel
{
    public $name = 'Post';

    public $belongsTo = 'User';

    public $hasMany = array('Answer');

    // Has many topics that belong to topic post join table... jazz
    public $hasAndBelongsToMany = array(
        'Topic' => array('with' => 'TopicPost')
    );
}

Topic.php
class Topic extends AppModel
{
    public $hasMany = array(
        'TopicPost'
    );
}

TopicPost.php
class TopicPost extends AppModel {
    public $belongsTo = array(
        'Topic', 'Post'
    );
}

When a user views a topic e.g. /topics/view/topicname I want to show all the posts that contain that Topic.

So far I have the following method in my TopicsController for the view:

public function view ( $slug )
{
    $topic = $this->Topic->find('first', array('conditions'=>array('Topic.slug'=>$slug)));
    $this->set('topic', $topic);
    $this->set('title_for_layout', $topic['Topic']['title'] . ' – Topics');

    $this->paginate = array
    (
        'Post' => array
        (
            'limit'=>15,
            'conditions'=>array
            (
                'Post.status'=>array(1,2),
                'TopicPost.topic_id' => $topic['Topic']['id'],
            ),
            'order'=>array('Post.datetime'=>'desc'),
            'contain'=>array('User'=>'Profile', 'TopicPost')
        )
    );

    $posts = $this->paginate('Post'); // this one

    $this->set('posts', $posts);

}

And so that I can use Posts and TopicPosts I have added: public $uses = array('Topic','TopicPost','Post'); to the top of the controller and made all models act as containable.

So basically I need to find Posts that have a match in the database model TopicPosts for the id of the topic I’m viewing.

  • 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-02T05:32:05+00:00Added an answer on June 2, 2026 at 5:32 am

    I just couldn’t get it to work the “proper” way. I’m not sure if this is a bug in cake or something, but the paginate function simply refuses to budge.. The proper way to do this would probably be to write your own paginate function in your Post model, there is some info on how to do that in the cookbook.

    Meanwhile, I offer you the workaround below. It’s not optimal (at least not without caching) but it works. You can do it the proper way when/if you run into performance problem, but until then, this code below should do it.

    public function view ( $slug )
    {
        $topic = $this->Topic->find('first', array('conditions'=>array('Topic.slug'=>$slug)));
        $this->set('topic', $topic);
        $this->set('title_for_layout', $topic['Topic']['title'] . ' – Topics');
    
        // step 1: get post IDs related to your topic
        $postIDs = $this->Topic->TopicPost->find
            (
                'list',
                array
                (
                    'fields'        => array('TopicPost.post_id'),
                    'conditions'    => array('TopicPost.topic_id' => $topic['Topic']['id'])
                )
            );
    
        $this->paginate = array
        (
            'Post' => array
            (
                'limit'=>15,
                'conditions'=>array
                (
                    'Post.status' => array(1,2),
                    // step 2: include them in your paginate conditions
                    'Post.id' => $postIDs,
                ),
                'order' => array('Post.datetime'=>'desc'),
            )
        );
    
        $posts = $this->paginate('Post');
    
        $this->set('posts', $posts);
    }
    

    (Please note that I’ve stripped some of the stuff in my tests as I didn’t have some of the stuff in your app, so don’t forget to put it back)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table from a vendor application that stores some xml data into
I have an application that stores images in a database. Now I have learned
Assume I have a application that stores data,gets data and processes data and stores
I have made a java application that stores data from a .csv file to
I have a multi-timezone web application that stores all of the datetime values in
I have a Windows.Forms based .NET desktop application that stores privileged information in a
I have to create a user in a third party application that stores its
I have an application that I'm converting to Symfony/Doctrine. It currently stores a serialized
I have recently written an application(vb.net) that stores and allows searching for old council
I have a C# Application I am creating that stores all data in SQL

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.