One way is to use the built-in counterCache Cake option in your association. This is probably the most efficient option, although this requires adding a field to the table.
In the stores table, add an INT field called product_count
In your Product model, add the counterCache parameter to your association:
var $belongsTo = array( 'Store' => array( 'className' => 'Store', 'foreignKey' => 'store_id', 'counterCache' => true ));
Whenever you add or remove Product records, it automatically updates the product_count field of the associated Store record, so there is no need to modify find operations.
Please note: if you choose this route, you need to manually update the product_count field so that the initial value is correct, since it is updated only after the add / remove operations.
Doug owings
source share