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

  • Home
  • SEARCH
  • 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 654037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:26:59+00:00 2026-05-13T22:26:59+00:00

I’m having trouble composing a CakePHP find() which returns the records I’m looking for.

  • 0

I’m having trouble composing a CakePHP find() which returns the records I’m looking for.

My associations go like this:

User ->(has many)-> Friends ,
User ->(has many)-> Posts

I’m trying to display a list of all a user’s friends recent posts, in other words, list every post that was created by a friend of the current user logged in.

The only way I can think of doing this is by putting all the user’s friends’ user_ids in a big array, and then looping through each one, so that the find() call would look something like:

$posts = $this->Post->find('all',array(
            'conditions' => array(
                'Post.user_id' => array(
                    'OR' => array(
                        $user_id_array[0],$user_id_array[1],$user_id_array[2] # .. etc
                    )
                )           
            )
        ));

I get the impression this isn’t the best way of doing things as if that user is popular that’s a lot of OR conditions. Can anyone suggest a better alternative?

To clarify, here is a simplified version of my database:

“Users” table
id
username
etc

“Friends” table
id
user_id
friend_id
etc

“Posts” table
id
user_id
etc

  • 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-05-13T22:26:59+00:00Added an answer on May 13, 2026 at 10:26 pm

    After reviewing what you have rewritten, I think I understand what you are doing. Your current structure will not work. There is no reference in POSTS to friends. So based on the schema you have posted, friends CANNOT add any POSTS. I think what you are trying to do is reference a friend as one of the other users. Meaning, A users FRIEND is actually just another USER in the USERS table. This is a self referential HABTM relationship. So here is what I would propose:

    1- First, make sure you have the HABTM table created in the DB:

    — MySQL CREATE TABLE users_users ( user_id char(36) NOT NULL,
    friend_id char(36) NOT NULL );

    2- Establish the relationships in the User model.

    var $hasAndBelongsToMany = array(
    'friend' => array('className' => 'User',
      'joinTable' => 'users_users',
      'foreignKey' => 'user_id',
      'associationForeignKey' => 'friend_id',
      'unique' => true,
      ),
    );
    
    var $hasMany = array(
    'Post' => array(
      'className' => 'Post',
      'foreignKey' => 'user_id'
    ),
    );
    

    3- use the scaffolding to insert a few records, linking friends and adding posts.
    4- Add the view record function to the Users controller:

    function get_user($id)
    {
      $posts = $this->User->find('first', array(
          'conditions' => array('User.id' => $id),
          'recursive' => '2'
      ));
      pr($posts);
    }
    

    5- Now you can query the User table using recursive to pull the records using the following command:

    http://test/users/get_user/USER_ID

    6- Your output will show all of the records (recursively) including the friends and their posts in the returned data tree when you pr($posts)

    I know this is a long post, but I think it will provide the best solution for what you are trying to do. The power of CakePHP is incredible. It’s the learning curve that kills us.

    Happy Coding!

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

Sidebar

Ask A Question

Stats

  • Questions 474k
  • Answers 474k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In short, the answer is no. May 16, 2026 at 4:13 am
  • Editorial Team
    Editorial Team added an answer Recursive structural types have never been supported. The -Yrecursion option… May 16, 2026 at 4:13 am
  • Editorial Team
    Editorial Team added an answer Probably menuOfSomeKind is TMenuItem and not TMainMenu If you are… May 16, 2026 at 4:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.