From 1173ee5efed75e1946e46b10fe9a55a8c83d1660 Mon Sep 17 00:00:00 2001 From: Ali1 Date: Sun, 30 Mar 2014 13:13:28 +0100 Subject: [PATCH] Fix notice when deleting last record When deleting the last record for a given foreign key, the $results (sum in my example) would be completely empty as there were no records to sum. --- Model/Behavior/AggregateCacheBehavior.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Model/Behavior/AggregateCacheBehavior.php b/Model/Behavior/AggregateCacheBehavior.php index 7ec5c33..e1a848d 100644 --- a/Model/Behavior/AggregateCacheBehavior.php +++ b/Model/Behavior/AggregateCacheBehavior.php @@ -83,8 +83,12 @@ function __updateCache(Model $model, $aggregate, $foreignKey, $foreignId) { foreach ($aggregate as $function => $cacheField) { if (!in_array($function, $this->functions)) { continue; - } - $newValues[$cacheField] = $results[0][$function . '_value']; + } + if (empty($results)) { + $newValues[$cacheField] = 0; + } else { + $newValues[$cacheField] = $results[0][$function . '_value']; + } } $assocModel->id = $foreignId; $assocModel->save($newValues, false, array_keys($newValues)); @@ -121,4 +125,4 @@ function afterDelete(Model $model) { } } -?> \ No newline at end of file +?>