开发者

CakePHP HABTM find all records that have associated records

I have a model - "Category" which HABTM "Blog". I need to create a query that will only select categories that have more zero blogs associated with them. I'm having t开发者_高级运维rouble working out how to do this. All I've come up with is getting the categories out of the database using find('all'...) and then extracting those that have something in the $category['Blog'] array. Obviously I'd much prefer to not have to ask the database to do all that work, so a more elegant solution would be much appreciated.

Thanks for reading!


not really elegant (short), but I think this is the correct way:

  1. add a blogs_category_count field to categories table
  2. add Category hasMany BLogsCategory with counterCache

so you just have to add a condition to that find('all')

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜