public function updateRisks($side, $risk_ids)
{
$current_risks = array();
$save_risks = array();
foreach ($this->risk_assignments as $curr) {
if ($curr->eye_id == $side) {
$current_risks[$curr->risk_id] = $curr;
}
}
// go through each update risk id, if it isn't assigned for this element,
// create assignment and store for saving
// if there is, remove from the current risk array
// anything left in $current_risks at the end is ripe for deleting
foreach ($risk_ids as $risk_id) {
if (!array_key_exists($risk_id, $current_risks)) {
$s = new OphCiExamination_InjectionManagementComplex_RiskAssignment();
$s->attributes = array('element_id' => $this->id, 'eye_id' => $side, 'risk_id' => $risk_id);
$save_risks[] = $s;
} else {
// don't want to delete later
unset($current_risks[$risk_id]);
}
}
// save what needs saving
foreach ($save_risks as $save) {
$save->save();
}
// delete the rest
foreach ($current_risks as $curr) {
$curr->delete();
}
}