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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:54:47+00:00 2026-05-25T15:54:47+00:00

I am building a friendship system in CakePHP that uses two tables: Users and

  • 0

I am building a friendship system in CakePHP that uses two tables: Users and Friends.

In Users (id,username,email,password) and Friends (id,user_from,user_to,status)

A user requests another user to be friends and this creates a record in the friends table storing both the user ids and setting a status of ‘Requested’. The user can either accept the friendship and the status changes to ‘Accepted’ or cancel the friendship and the record is deleted from the database.

An example link for the request looks like and could be shown either in a users list or on the users details page:

<?php echo $this->Html->link('Add as Friend', array('controller'=>'friends','action'=>'add_friend',$user['User']['id'])); ?>

Question 1 is how could I make this link change to a cancel request link if the user has a request against them or is already friends?

This link corresponds to the following method in the controller:

function add_friend ( $id )
    {
        if(!empty($this->data))
        {
            $this->Friend->Create();
            if($this->Friend->save($this->data))
            {
                $this->Session->setFlash('Friendship Requested');
                $this->redirect(array('controller'=>'users','action'=>'login'));
            }
        }
    }

So we are passing the ID to the method which will be the user_to and then the ‘user_from’ needs to be the currently logged in user and set the status to ‘Requested’. Question 2 is how to do I do this? Also how do I prevent a user from creating multiple records by just calling that method over and show a message saying you’ve already requested friendship.

The next method is:

function accept_friendship ( $id )
    {
     $this->Session->setFlash('Friendship Accepted');
                $this->redirect(array('controller'=>'friends','action'=>'index'));
            }
        }
    }

Question 3: But again I’m confused as to how I would change the status of the record and mark the users as friends when the method is called. Also need to prevent this from being called multiple times on the same record.

The final bit is listing the friends for the user or another user:

function users_friends( $id )
{
 $this->set('friends', $this->Friend->find('all'));
}

function my_friends()
{
$this->set('friends', $this->Friend->find('all'));
}

As you can see the first method requires the id of the user you are viewing and then the second method will use the currently logged in user id. Question 4: How do I then use this to list the friends of that user?

If anyone can help put me on the right track with this it’d be much appreciated as I’ve ground to a halt and not sure how to do those 4 things and trying to learn CakePHP as best I can so help is much appreciated. Thanks

EDIT: It has occurred to me that a view with hidden fields could be used to store the information regarding the friend request that the user confirms but this isn’t ideal as it means sending the user off somewhere else when in fact I want to just run the function and do the redirect straight off. NOT AJAX THOUGH!

  • 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-25T15:54:48+00:00Added an answer on May 25, 2026 at 3:54 pm

    Answer 3 is something like this:

    function accept_friendship ( $id ) {
        $this->Friend->id = $id;
        $current_status = $this->Friend->field('status');
    
        if($current_status=='Requested') {
            $this->Application->saveField('status', 'Accepted');
        }
    
        $this->Session->setFlash('Friendship Accepted');
        $this->redirect(array('controller'=>'friends','action'=>'index'));
    }
    

    Essentially get the ID of the friend request, check the status field, and if it’s equal to Requested, then update it to Accepted. This way it will only be called once.

    And also to prevent people from repeatedly “accepting” a friend, just remove the “Accept” link once it’s been accepted. The if statement stops your code from updating unnecessarily.

    You should also put some kind of prevention in place so that only the requested friend can accept the request. Otherwise I could type the URL yoursite.com/friends/accept_friendship/123 and accept a random persons request without any authentication.

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

Sidebar

Related Questions

No related questions found

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.