cwbit / cakephp-aggregate-cache

A Behavior plugin for CakePHP that extends the idea of counterCache and counterScope to more fields
8 stars 4 forks source link

Implement configurable default values for functions #4

Open cwbit opened 10 years ago

cwbit commented 10 years ago

Issue #1 brought up the issue where an aggregate field might not have a value if no child records exist (e.g. you just deleted the last child record).

The temporary solution to this was to set the aggregate field value to ZERO if no records came back for the aggregate function.

A better solution would be to implement configurable defaults for each aggregate function instead of forcing ZERO if no values come back.

Imagine I was trying to track the AVG of something - I would rather have it come back as NULL if no records exist than to set it as 0 which might imply records exist but just happen to average out to 0 (which might not always be the case).

Indemnity83 commented 10 years ago

Couldn't you just cache the count along side your avg and check that count > 0 in code to handle the edge case of zero child records?

Seems like this may not be a bug/enhancement necessary in code, just a documentation clarification.