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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:47:05+00:00 2026-06-10T22:47:05+00:00

I have Two diff tables as given below: users and posts Need data from

  • 0

I have Two diff tables as given below:

users and posts

Need data from user’s table order by count of posts table

Relationship is defined as:

User Model:

public $hasMany = array('Post');

Post Model

Public $belongsTo = array('User');
  • 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-10T22:47:07+00:00Added an answer on June 10, 2026 at 10:47 pm

    counterCache – Cache your count()

    This function helps you cache the count of related data. Instead of counting the records manually via find(‘count’), the model itself tracks any addition/deleting towards the associated $hasMany model and increases/decreases a dedicated integer field within the parent model table.

    The name of the field consists of the singular model name followed by a underscore and the word “count”:

    my_model_count
    

    Let’s say you have a model called ImageComment and a model called Image, you would add a new INT-field to the image table and name it image_comment_count.

    Once you have added the counter field you are good to go. Activate counter-cache in your association by adding a counterCache key and set the value to true:

    <?php
    class Image extends AppModel {
        public $belongsTo = array(
            'ImageAlbum' => array('counterCache' => true)
        );
    }
    

    From now on, every time you add or remove a Image associated to ImageAlbum, the number within image_count is adjusted automatically.

    You can also specify counterScope. It allows you to specify a simple condition which tells the model when to update (or when not to, depending on how you look at it) the counter value.

    Using our Image model example, we can specify it like so:

    <?php
    class Image extends AppModel {
        public $belongsTo = array(
            'ImageAlbum' => array(
                'counterCache' => true,
                'counterScope' => array('Image.active' => 1) // only count if "Image" is active = 1
        ));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two data tables and would like to join the two tables to
I have below information in table and want to retrive the count if difference
I have table structure like below: events (boxing, sparring etc) competitors (users who are
I have two tables of ID's and dates and I want to order both
I have two tables: products products_tmp The products_tmp table has less fields. But all
I need to compare numeric data from two datatables with the same schema. For
I have two tables images2 and image_data So the goal is to have 1
I have two classes (MVC view model) which inherits from one abstract base class.
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4
I have two tables, lets say table1 and table2 with common columns, id and

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.