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

The Archive Base Latest Questions

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

I am using Cakephp 2.0 My model relationship as follows: User has many Post

  • 0

I am using Cakephp 2.0 My model relationship as follows:

User has many Post

Post has many Comment

Post has many Like

Comment has many Like

In Post table I added a column “like_count” and enable the countercache
and
In Comment table I added a column “like_count” and enable the countercache

Likes table contain:

id,post_id,comment_idu,user_id

and Model as follows :

public $belongsTo = array(
    'Post' => array(
        'className' => 'Post',
        'foreignKey' => 'post_id',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'counterCache' => true
    ),
    'Comment' => array(
        'className' => 'Comment',
        'foreignKey' => 'comment_id',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'counterCache' => true
    ))

Its working Fine..

But Now I need to do total like count(Post like count + Comment like
count) for each user. so I planned to add a field in users table and
enable the countercache But totally strucked.. what will be the column
name i need to add in users table and where i need to put Countercache
=> true. I dont how to go further..

  • 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-01T20:49:10+00:00Added an answer on June 1, 2026 at 8:49 pm

    Add the following to your $belongsTo array.

        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'counterCache' => true
        ),
    

    And you’ll add a like_count column in the users table.


    And in your UserModel you’ll need to add a HasMany for likes of course.

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

Sidebar

Related Questions

I'm using cakephp and trying to check if a table has data entered in
I am using CakePHP 2.0 and I have a many-to-many relationship betweens users and
I'm using CakePHP 2.0. I have 2 models/controllers (Category/Coupon): // File: Model/Coupon.php // Table
I have created a temporary table using cakephp $this->Model->query() and inserted some records using
I have a CakePHP model - User - that has ties to an external
I'm using cakephp, I have a model comment with two fields : model_type and
I'd like to insert data into a mysql table using 'the cakephp way'. I
Using CakePHP 1.3 I have a fairly large model in CakePHP, and I'd like
I am learning the Model relationship types in cakephp. I have built two tables
Using cakephp, I have a generic address table, which I want to link to

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.