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'); 回答1: 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