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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:55:09+00:00 2026-06-02T00:55:09+00:00

In my app I have three tables: Users Profiles Friends The User has one

  • 0

In my app I have three tables:

Users
Profiles
Friends

The User has one Profile and Profiles have one User and have many Friends. Friends have many Profiles.

In the Profile model I have a method that gets a list of Profiles for a Profile (BUT it uses the related username which comes from the user table.)

public function getFriends($username) {

   return $this->find('all', array(
     'conditions' => array(
       'User.username' => $username,
        'Friend.status' => 1
      )
   ));

}

So it should be getting a list of profiles that match where the username matches a User and Friend status is 1. How do I do this though?

I’ll also post my associations so you can understand the DB:

User Model:

public $hasOne = 'Profile';

Profile Model:

public $belongsTo = 'User';

public $hasMany = array(
    'ProfileFrom'=>array(
        'className'=>'Friend',
        'foreignKey'=>'profile_from'
    ),
    'ProfileTo'=>array(
        'className'=>'Friend',
        'foreignKey'=>'profile_to'
    )
);

public $hasAndBelongsToMany = array(
    'Friendship' => array(
        'className' => 'Profile',
        'joinTable' => 'friends',
        'foreignKey' => 'profile_from',
        'associationForeignKey' => 'profile_to'
    )
);

public $actsAs = array('Containable');

Friend Model:

public $belongsTo = array(
      'UserFrom'=>array(
         'className'=>'Profile',
         'foreignKey'=>'profile_from'
      ),
      'UserTo'=>array(
         'className'=>'Profile',
         'foreignKey'=>'profile_to'
      )
   );

public $actsAs = array('Containable');
  • 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-02T00:55:11+00:00Added an answer on June 2, 2026 at 12:55 am

    This looks complicated, I’d suggest having a look at Containable (a core cakephp behavior) which makes selecting which data to fetch a lot easier (especially with regards to preventing selecting too much data). Please consider using that for your whole app.

    It seems you’re trying to tell cake to search in associated Friend model, but if I’m right the only associations existing to the friend are aliased as ProfileTo and ProfileFrom. So I’m guessing cake won’t know where to fiend ‘Friend’. But in the future it would be helpful to post any errors as well.

    See my suggestion below for a possible fix.


    For now you could do the following:
    In ProfileModel:

    function getFriends($username) {
        //Start by getting User's ID
        $user = $this->User->find(
            'first',
            array(
                'conditions' => array(
                    'User.username' => $username
                )
            )
        );
        //User's ID is now $user['User']['id']
    
        //Fetch user's profile with associated friends
        $this->Behaviors->attach('Containable');
        $profileAndFriends = $this->find(
            'first',  //Because User hasOne Profile, so we'll fetch this one profile.
            array(
                'conditions' => array(
                    'Profile.user_id' => $user['User']['id']
                ),
                //We'll want to fetch all associated Friend records (which are linked to the Profile model as ProfileFrom and ProfileTo
                'contain' => array(
                    'ProfileFrom' => array(
                        'conditions' => array(
                            'status' => 1
                        )
                    ),
                    'ProfileTo' => array(
                        'conditions' => array(
                            'status' => 1
                        )
                    )
                )
            )
        );
        return $profileAndFriends;
    }
    

    It should work like this. So in FriendsController you should now be able to do $this->Friend->UserTo->getFriends('cameron');
    That should fetch the profile belonging to the user with username ‘cameron’, and all Friends associated with this profile.


    There’s one small thing still bothering me though, and that’s the fact that you got an error stating that ProfileTo isn’t associated with Profile, which is weird as in your example that relation is obviously defined.
    The cause might be a fault in the definition of your associations, but I’m not sure.


    One more suggestion, if the above code doesn’t work, replace the contain array with:

                'contain' => array(
                    'Friendship' => array(
                        'conditions' => array(
                            'OR' => array(
                                'Friendship.profile_from' => 'Profile.id',
                                'Friendship.profile_to' => 'Profile.id',
                            )
                        )
                    )
                )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables in my app 1. Users, 2. Restaurants. A user can
I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares. if suppose, Users table having 6 user
I have a service monitor app that monitors the status of three other servers
I have a winforms app. I give it to three clients and each one
I have an Android project that branched into three different applications, app-1 , app-2
I have an app that has a TableView, NavigationView and TabBar running together. There
Are there any DHTMLX users out there? We have an app that is being
I have a rails app which is already running with some users, users that
I'm on a pseudo-social app. We have 3 tables: Users: _id_, username, password, email
I am creating an iPhone App. I have some tables to save users information.

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.