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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:28:32+00:00 2026-06-12T22:28:32+00:00

I have 3 tables: – users – posts – Subscriptions (which should be by

  • 0

I have 3 tables:
– users
– posts
– Subscriptions (which should be by default posts_users (n-n))

The subscriptions table is the n-n table needed for this relation. I didnt want to call it “posts_users” as it is very confusing for my system because i have more relations between users and posts.

The, to create the hasAndBelongsToMany relation, i did this:

Post (model):

    public $hasAndBelongsToMany = array(
        'Subscriptions' => array(
                'className' => 'User',
                'joinTable' => 'subscriptions',
                'foreignKey' => 'post_id',
                'associationForeignKey' => 'user_id',
                'unique' => 'keepExisting',
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'finderQuery' => '',
                'deleteQuery' => '',
                'insertQuery' => ''
        )
);

User (model):

    public $hasAndBelongsToMany = array(            
    'Post' => array(
        'className' => 'Post',
        'joinTable' => 'subscriptions',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'post_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

Lets say i want to find the subscribed posts for the user with id 3.
Is there any way to do a find to retrieve data of subscribed Posts for a user?
In which model should i do the query? How??

Thanks.

  • 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-12T22:28:33+00:00Added an answer on June 12, 2026 at 10:28 pm

    Ok. Finally i got it.

    You can do the query from any of the two related models. In this case Post or User.

    $this->Post->User->find('all', array(
                'conditions' => array('User.id' => $this->Session->read('Auth.User.id')),
                'contain' => array('Subscriptions')
    ));
    

    This would return the subscribed tickets (in the subscriptions array) for the current user.

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

Sidebar

Related Questions

have tables like this Table checklist idCardno name permAddress datetime Table persons name idcardno
I have tables & data like this: venues table contains : id +----+---------+ |
I have tables which looks like below. Employee table Date Employee ID Employer ID
If I have tables like this: ImportSet1.Users ImportSet2.Users ImportSet3.Users Then in my sproc I
I have tables in a MySQL database like this... CREATE TABLE `someUserTable` ( userId
I have tables which looks like this: text = ID = 1234 Hello World
I have tables as below. Table Messages message_id parent_id forum_id user_id Table Users user_id
I have tables like changes201101 changes201102 changes201103 ... changes201201 And table whichchanges which contain
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity

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.