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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:28:49+00:00 2026-06-01T16:28:49+00:00

I have the following tables for my CakePHP app that allows friendships between users:

  • 0

I have the following tables for my CakePHP app that allows friendships between users:

**Users**
id
username
password

**Profiles**
id
firstname
lastname
user_id

**Friends**
id
user_id_to
user_id_from
status

So basically a user has a profile and a user can be friends with another user and this is recorded in the database table called friends with a simple status of confirmed or not using either 0 or 1 (it’s an int). So friends is the join between two users.

I’m trying to list the friends for a user so for example if I get a url like:

/people/cameron/friends it will list the friends for the user Cameron.

However I’m struggling with the find statement to pass the user and find them (notice I contain the profile data) and then list friends that are related to that user. Can anyone help?

These are the Friend, User and Profile models:

class Friend extends AppModel
{
    public $name = 'Friend';

    public $belongsTo = array('User');

    public $actsAs = array('Containable');

}

User.php

   class User extends AppModel
    {
        public $name = 'User';

    public $hasOne = 'Profile';

    public $hasMany = array(
        'Post',
        'Answer',
        'Friend' => array(
            'foreignKey' => 'user_id_to'
        )
    );

public $belongsTo = array(
        'Friend' => array(
            'foreignKey' => 'user_id_from'
        )
    );

    public $actsAs = array('Containable');

        public function getFriends($username)
        {
        return $this->find('all',
        array('conditions' => array('User.username' => $username, 'Friend.status'=>1),
            'contain' => array('Friend' => array('User'))
        ));
        }
    }

Profile.php

class Profile extends AppModel
{
    public $name = 'Profile';

    public $belongsTo = 'User';

    public $actsAs = array('Containable');
}

and this is my method for showing the friend list for a user:

public function index( $username )
{   
    $friends = $this->User->getFriends($username);

    $this->set('friends', $this->paginate());
}

I’m currently getting this error:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'User.user_id_from' in 'on clause'
SQL Query: SELECT `User`.`id`, `User`.`username`, `User`.`password`, `User`.`email`, `User`.`status`, `User`.`code`, `User`.`lastlogin`, `Friend`.`id`, `Friend`.`user_id_from`, `Friend`.`user_id_to`, `Friend`.`datetime`, `Friend`.`status` FROM `db52704_favorr`.`users` AS `User` LEFT JOIN `db52704_favorr`.`friends` AS `Friend` ON (`User`.`user_id_from` = `Friend`.`id`) WHERE `User`.`username` = 'cameron' AND `Friend`.`status` = 1

It looks as though the app thinks the foreign keys are in the User table rather than the friend table even though they called within the Friend association… Any ideas what the problem is?

  • 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-01T16:28:50+00:00Added an answer on June 1, 2026 at 4:28 pm

    Any time you specify foreign keys for $belongsTo, remember that the name you specify is the name of the field in the current table, not the other table.

    So, for example, your $belongsTo references to 'foreignKey' => 'user_id_to' should be in the Friends model, not the Users model.

    Re-read Cake’s docs, as it does get confusing (even after years of Cake apps, I still need to refresh when I start a new project): http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

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

Sidebar

Related Questions

I have the following tables: People Users Emails The user's password is stored in
I am using CakePHP 2.0's integrated Auth component. I have the following tables :
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
I have the following tables in my database that have a many-to-many relationship, which
I have following tables: users(id, name) posts (id, text, userId) comments (id, text, userId,
I have built a CakePHP app that creates appointments which have both a client
I have following tables : users accounts ( user has_one account ) pictures (
Have the following tables: Clients (ID, LastName) Orders (ID, ClientID) Payments (ID, OrderID, PaymentDate,
I have the following tables in MySQL: team: id, name, [more stuff] person: id,
I have the following tables: 1-Categories: -CategoryID -CategoryName -ParentID 2-Items: -ItemId -ItemName -CategoryID categories

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.