tags:

views:

26

answers:

2

I'm rebuilding one of my sites using the CakePHP Framework (awesome!), and I need to limit the number of hasMany and HABTM records that a Model can have. Let's say for "Profile hasMany Image," I want to limit a profile to only 10 images.

I know I can easily accomplish this using the Find method with 'count,' but I'm wondering if there's a property that can be set somewhere that I overlooked. I've looked at the CakePHP documentation and Googled a bit, haven't seen anything, but I'm always running across little CakePHP secrets now and then.

A: 

The only thing that might make it slightly easier than $this->Model->find('count') is to implement counterCache.

http://book.cakephp.org/view/1033/counterCache-Cache-your-count

But I don't think this works with HABTM relationships. I'm fairly certain there's no built-in way to limit the number of associated records with a simple model attribute or anything like that. The amount of code you'll have to write is fairly trivial though.

handsofaten
Thanks. counterCache looks awesome for the hasMany's. Will have to type up a little more code for the HABTM's!
whelanska
+1  A: 

I wrote a HabtmCounterCache Behavior for CakePHP. Let me know what you think.

neilcrookes
Yes! Haven't tried it in my app yet, but this could be perfect. Thanks for the link (and the great work).
whelanska