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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:21:41+00:00 2026-06-16T00:21:41+00:00

I have a simple friends system built in CakePHP with two tables: Users and

  • 0

I have a simple friends system built in CakePHP with two tables: Users and Friends

Users
id,
username,
password

Friends
id
user_id
friend_id
status

To list the friends for a user I do the following:

$user = $this->User->find('first', array( 'conditions' => array('User.username' => $username)));

// First get a list of friend ids
$friendsList = $this->Friend->find('list', 
    array(
        'conditions'=>array(
           'Friend.user_id'=>$user['User']['id'],
           'Friend.status'=>1
        ),
        'fields'=>array(
            'Friend.friend_id'
        )
    )
);

// Then list users with matching ids
$friends = $this->User->find('all', 
    array(
        'conditions'=>array(
           'User.id'=>$friendsList
        ),
    )
);

Now this works fine for listing friends that the user has friended as it’s pulling the list of friend ids that the user id matches in the friends table. The problem is if a friend has friended the user then the ids will be reversed in the columns and therefore it wont show the friendship when viewing one user but will the other… If that makes sense?

How do I fix this? I basically need to find all matching ids in both columns for the user and then pull out the friend id or user id that matches the row.

I’ve posted the models as well (although I don’t think they really matter for this question as the issue is with the query rather than the relationships which are working).

User Model:

 public $hasAndBelongsToMany = array(
        'User'=>array(
            'className'              => 'User',
            'joinTable'              => 'friends',
            'with'                   => 'Friend',
            'foreignKey'             => 'user_id',
            'associationForeignKey'  => 'friend_id'
        )
 );

Friend model is virtualised!

Thanks

EDIT: Doing this fixes the problem:

// First get a list of friend ids
$friendsList1 = $this->Friend->find('list', 
    array(
        'conditions'=>array(
           'Friend.user_id'=>$user['User']['id'],
           'Friend.status'=>1
        ),
        'fields'=>array(
            'Friend.friend_id'
        )
    )
);

$friendsList2 = $this->Friend->find('list', 
    array(
        'conditions'=>array(
           'Friend.friend_id'=>$user['User']['id'],
           'Friend.status'=>1
        ),
        'fields'=>array(
            'Friend.user_id'
        )
    )
);

$friendsList = array_merge($friendsList1,$friendsList2);

// Then list users with matching ids
$friends = $this->User->find('all', 
    array(
        'conditions'=>array(
           'User.id'=>$friendsList
        ),
    )
);

But the performance could be rather poor if I have a large DB of users I presume.

  • 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-16T00:21:41+00:00Added an answer on June 16, 2026 at 12:21 am

    I think you want this to be a recursive table so that the model works correctly.
    You essentially want User to habtm User. So your model should look more like this.

     public $hasAndBelongsToMany = array(
            'User'=>array(
                'className'              => 'User',
                'joinTable'              => 'users_user',
                'foreignKey'             => 'user_id',
                'associationForeignKey'  => 'friend_id',
                'fields'                 => array('id','user_id','friend_id'),
            )
     );
    

    Then you can query the User model and get all the relations as they should be. like so:

    $friends = ($this->User->Friend->find('all', array('conditions'=>array(
        'Friend.id'=>$loggedinuserid,
                        "OR" => array (
                         "User.id" => $loggedinuserid,
                        ) 
    ))));
    

    Or if your original model works:

    $friendsList = $this->Friend->find('list', 
        array(
            'conditions'=>array(
               'Friend.status'=>1,
               "OR"=>array(
                     array('Friend.friend_id'=>$user['User']['id']),
                     array('Friend.user_id'=>$user['User']['id'])
                    )
            ),
            'fields'=>array(
                'Friend.user_id'
                'Friend.friend_id'
            )
        )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

friends, i have a EditText on simple activity with a button. when every i
I have the following tables users: uid, name_f, name_l... group_data: id, group_id, admin, invitedusers,
friends, I have a piece of code which can perform simple add, subtraction, multiplication,
I have a simple demo app like this Friend = Backbone.Model.extend name:null Friends =
Example: I have a list of friends which I retrieve as XML from a
Guys I m using a simple listview. I have two activities one main and
friends, I have a simple script import subprocess subprocess.call([./run_xf]) old=open('./inv.mt0','r') lines=old.readlines() lines=lines[3:] new=open('./inv.mt1','w') new.writelines(lines)
So I have made a simple site for a friend using codeigniter and grocery
I have three classes, I am simply display my Facebook friends in tableView. And
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (

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.