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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:16:05+00:00 2026-05-17T06:16:05+00:00

I have a table called user_relationship. which has two foreign keys refering back to

  • 0

I have a table called user_relationship. which has two foreign keys refering back to the User table to map that they are friends.

CREATE TABLE `user_relationships` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `status` varchar(255) default 'pending',
  `time` datetime default NULL,
  `user_id` int(11) unsigned NOT NULL,
  `friend_id` int(11) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `fk_user_relationships_users1` (`user_id`),
  KEY `fk_user_relationships_users2` (`friend_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

when i try to bake it naturally doesnt understand that the friend_id has to refer to User module. i wanna manual edit the code but o have some problem understanding what parts to edit in the following

var $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Friend' => array(
        'className' => 'Friend',
        'foreignKey' => 'friend_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

In this part of the code i wanna refer the friend_id to the User Table

    'Friend' => array(
        'className' => 'Friend',
        'foreignKey' => 'friend_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )

i tried doing this.. do i need to change anything else ?

    'Friend' => array(
        'className' => 'Friend',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
  • 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-17T06:16:05+00:00Added an answer on May 17, 2026 at 6:16 am

    How about this?

    class UserRelationship {
    
        var $belongsTo = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id'
            ),
            'Friend' => array(
                'className' => 'User',
                'foreignKey' => 'friend_id'
            )
        );
    
    }
    
    class User {
    
        var $hasAndBelongsToMany = array(
            'Friend' => array(
                'className' => 'User',
                'joinTable' => 'user_relationships',
                'foreignKey' => 'user_id',
                'associationForeignKey' => 'friend_id',
                'with' => 'UserRelationship'
             )
        );
    
    }
    

    Associations can be seen from different points of view:

    User        <-    Relationship    ->     User
     |                     |                  |
     hasMany           belongsTo              |
     Relationship  <-  User (user_id)        hasMany
     |                 User (friend_id)  ->  Relationship
     |                                        |
     HABTM                                   HABTM
     User       <---------------------->     User
    

    Your “physical” layout, i.e. database schema, is User -> Relationship -> User. Your real desired relationship is a User -> User relationship though. Technically that translates into a User hasAndBelongsToMany User association (also called many-to-many). Since it uses three models, a many-to-many association can be broken down into:

    • User hasMany Relationship/Relationship belongsTo User
    • Relationship belongsTo User/User hasMany Relationship

    You don’t need the hasAndBelongsToMany association, but it’s what you actually want. You don’t really need the Relationship model at all, since it’s just a helper model to connect the two Users, but if you do want to specify its associations as well, it has two belongsTo associations.

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

Sidebar

Related Questions

I have a table called BlogPost which has a 1-to-many relationship with the Comment
Suppose I have a table called Companies that has a DepartmentID column. There's also
Right now I have a table called Campaigns that has many Hits, if I
I have a table called ApprovalTasks... Approvals has a status column I also have
i have a table called category in which i have main category ids and
Say I have a table called xml that stores XML files in a single
I have a database table called Posts which stores all the information regarding an
I have an entity called User which can have a list of other Users
suppose i have a entity called USER and a relationship FRIENDSHIP exist between two
I have a table called order which contains columns id , user_id , price

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.