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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:57:51+00:00 2026-05-29T06:57:51+00:00

I’ve been trying to figure out for a week or so, how I can

  • 0

I’ve been trying to figure out for a week or so, how I can make a proper friendship system in CakePHP. I’ve read this and this thread but I can’t get it to work.

I’ve read a lot more threads regarding this, but nobody seems to have a proper example.

I currently have a table users (id, username, password, e-mail etc.) and a table friendships (id, user_to, user_from, status).

Step 1 – Friendship request

If a user does a friendship request, then a row is inserted with the requesting user_id and the user_id of the user from whom the friendship is request, so it could look like:

id | user_from | user_to| status


1 | 1 | 2 | 0

This way I can easily show pending friends of user_id = 2, by selecting all records where user_to = 2

Step 2 – Confirm friendship

I’ve set it up so that user_id 2 now sees that user_id 1 wants to become friends, if he clicks the confirmation link, the status will be changed to 1, see below

id | user_from | user_to| status


1 | 1 | 2 | 1

I created all kinds of checks so the row stays unique.

Step 3 – Show friends

I thought this would be easy, if I want to show the friends of user_id = 1 then I just do a select with user_from = 1 OR user_to = 1, however this doesn’t work.

User_id 1 can be a requester but can also be requested, so a JOIN will show strange results.

Does anyone know a solution? I’m happy to rebuild the entire system if I’m not doing the entire thing right! Any hints in the right direction are welcome as well…

  • 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-29T06:57:52+00:00Added an answer on May 29, 2026 at 6:57 am

    Here is my solution : the difficulty lies in the correct request because friend requests can be crossed (if A asks B or B asks A will be stored the opposite way in the “to” and “from” fields of the table). Lets do it like that and user UNION and aliases to get friends from any user independently of the relation table bellow.

    1. The [friends] table (relation): to|from|statut(pending,confirmed)
    2. “to” and “from” > foreign_keys constraint to a [users] table

    The request below always gives the wanted results ! (replace %d by the user ID or user Id in the SESSION

    SELECT
        users.userNickname,
        friends.to AS friendUser,
        friends.from AS currentUser,
        friends.statut
        FROM
        users
            INNER JOIN
            friends
            ON
            users.userId = friends.to
        WHERE
        friends.from = '%d' 
    UNION
        SELECT
        users.userNickname,
        friends.from AS friendUser,
        friends.to AS currentUser,
        friends.statut
        FROM
        users
            INNER JOIN
            friends
            ON
            users.userId = friends.from
        WHERE
        friends.to = '%d'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.