Order data based on count of related table data


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');


<strong>counterCache - Cache your count()</strong>

This function helps you cache the count of related data. Instead of counting the records manually via <strong>find('count')</strong>, the model itself tracks any addition/deleting towards the associated <strong>$hasMany</strong> 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”:


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

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

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

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

You can also specify <strong>counterScope</strong>. 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 )); }


  • How can I convert this tuple of tuples into a count of its elements?
  • Python : number of characters in text file
  • How to reduce the time delay to reach run method of Runnable class using ExecutorService Java
  • How does this Java code which determines whether a String contains all unique characters work? [dupl
  • something very wrong with SESSIONS
  • Fluent NHibernate automap list of strings with nvarchar(max)
  • Gem not installing package
  • Java text printing framework
  • how to bypass pinentry (passphrase screen) while decrypting a file using gpgme
  • joomla! 1.5.26 and “You are currently logged in to the private area of this site”
  • Extract data between rows r
  • PHP - Can't dynamcally instantiate class
  • Grid creating extra spacing that I don't want
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • Iterate twice through a DataReader
  • Update varbinary(MAX) field in SQLServer 2012 Lost Last 4 bits
  • Android Chronometer starts and stops but carries on counting when stopped
  • SQL: Getting the physical size of a subset of a table
  • HttpClient: disabling chunked encoding
  • How do I remove all but some records based on a threshold?
  • How do I get the list of bad records that didn't load in Bigquery?
  • Clear fused location provider's location for testing
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • Why cepheus don't send int without quotes to orion?
  • how does System.Web.HttpRequest::PathInfo work?
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • Sequential (transactional) API calls in angular 4 with state management
  • How VBA declared Volatility works
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Test if a set exists before trying to drop it
  • Email verification using google app script and google forms
  • Chrome doesn't support silverlight anymore? How to solve this?
  • how does django model after text[] in postgresql [duplicate]
  • Cant find why the layout is getting smaller
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?