2010-04-29 122 views

回答

0

是的,你是對的。你應該在控制器中獲取數據,然後在視圖中顯示它。你在問題中描述的與example in the cookbook完全相同。假設你與Post hasMany Comment有關,可以找到計數使用:

$comment_count = $this->Post->Comment->find('count',array('conditions'=>array('Comment.post_id'=>$postid))); 
3

另一個選項是cache your count。通過這種方法,你會現場comment_count添加到您的posts表,然後修改Comment模型belongsTo協會這樣的:

​​

每當一個新Comment記錄被創建時,相關Post記錄的comment_count遞增並且在關聯的Comment被刪除時遞減。

相關問題