Counts for Associated Tables (hasMany)

I'd like some advice on the CAKE way of getting a count of an associated (recursion 1) table, and paginating that. e.g.

Recipe belongsTo Author
Recipe hasMany Comments

in my recipes_controller I have a function recentRecipes()` that will perform a

$this->Recipe->recursive = 1;
$this->paginate = array('conditions' => $conditions);
$this->set('recipes', $this->paginate());

In the associated view, I'd like to show:

Recipe Name, Author Name,Number of Comments

I have achieved the number of comments by using recursion 1, then using

foreach ($recipes as $recipe):
  echo sizeof($recipe['Comment'])
endforeach;

1. I don't that the way I've done this with recursion=1 and sizeOf is right....I feel I should be using find('count') or should I be writing the more complicated query (which possibly reduces the benefit of cakephp) that incorporates a calculated count into the result set .....Advice?

2. How on earth do I use sort for pagination of this field ?

Asked by twistedpear, on 19/1/10

1 Answer

counter cache is what you need

Answered by dogmatic69on 8/2/10

<< previous next >>

Your Answer

You can use Creole Wiki Syntax to format your text.

Tagged with

Rating

0

Viewed

264 times

Last Activity

on 8/2/10