Ok.. You could do it in a trigger. Keep in mind that if you were to make changes outside of Xataface the trigger wouldn't be fired and your totals could go out of sync.
Issues to consider in the trigger:
1. If a donation amount is changed, then you need to be able to update the total accordingly.
2. If a donation is deleted, then you need to update the total accordingly.
Hence, what I would do is run the total calculation each time a donation is added, edited, or deleted. This will ensure that it keeps it in sync (rather than just dealing with the increment).
For this I would use the afterSave() and afterDelete() triggers in the donations table.
You could create a function say (update donations) that would update the donations totals. Then call this function from the afterSave() and afterDelete() triggers.
e.g.
- Code: Select all
function updateDonationsFor($donateurID){
$res = mysql_query("select sum(donationAmount) from donations where donateurID='".addslashes($donateurID)."'", df_db());
if ( !$res ) throw new Exception(mysql_error(df_db()));
list($totalDonations) = mysql_fetch_row($res);
@mysql_free_result($res);
$res = mysql_query("update donateurs set totalDonations='".addslashes($totalDonations)."' where donateurID='".addslashes($donateurID)."' limit 1", df_db());
if ( !$res ) throw new Exception(mysql_error(df_db()));
}
Then your donations delegate class would contain triggers:
- Code: Select all
function afterSave(&$record){
updateDonations($record->val('donateurID'));
}
function afterDelete(&$record){
updateDonations($record->val('donateurID'));
}
}