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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:29:40+00:00 2026-06-13T01:29:40+00:00

I have Posts and Comments and users can Like either. I’m using cakePHP. The

  • 0

I have Posts and Comments and users can “Like” either. I’m using cakePHP.

The Posts and Comments tables each have a ‘likes’ row on them because I don’t want to re-count the likes each time the post / comments are loaded. I have a Likes table too, that contains the IDs (post id, user id) so that I know what users have already ‘liked’ something.

I was wondering how I would set up this relationship within the models in cakePHP and also how I would update Posts.likes field when at the same time adding a new like into the Likes table.

I’ve set up Likes to “belongTo” Posts and Comments in the Like Model and at the moment, my LikesController.php looks like this:

public function add(){
...
    if ($this->Like->save($this->request->data)) {
      //like is added to Likes table, now how to add to the "parent" Post or Comment??
    }
...
}
  • 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-13T01:29:43+00:00Added an answer on June 13, 2026 at 1:29 am

    Keep your tables as they are, but add a like_count field to your posts and comments tables.

    Also add a comment_count to the posts table.

    Then just use CakePHP’s counterCache, and it will keep track of the # of likes and comments per post automatically.

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

Sidebar

Related Questions

I have users, posts and comments. User can post only one comment to each
A user can create a post. Posts have comments. A comment must belong to
I have an application with Users, Posts and Comments. Users has_many Posts, Posts has_many
Okay i have two models: posts and comments. as you can think comments has
In a blog in Rails I want to have paths like http://mydomain.com/posts/28383/comments#21 This is
Assuming I have a comments model and a posts model, What code can I
I have the following tables: posts (post_id, content, etc) comments (comment_id, post_id, content, etc)
I have a web application where users can add recommendations , post comments etc
I have a forum where users can post comments. When a comment is created
I have a web app where users can post comments to it. Is there

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.