public function updateDisorders($side, $data)
{
if (!in_array($side, array('left', 'right'))) {
throw new \Exception("invalid side specification");
}
$eye_id = $side == 'left' ? \Eye::LEFT : \Eye::RIGHT;
// ensure we're manipulating what is currently in the db
$current = $this->getRelated($side . "_cvi_disorder_assignments", true);
// if the element has been saved before, then the assignment values will exist
// and we can update, or delete those that are no longer required.
while ($assignment = array_shift($current)) {
if (array_key_exists($assignment->ophcocvi_clinicinfo_disorder_id, $data)) {
$this->updateDisorderAssignment($assignment, $data[$assignment->ophcocvi_clinicinfo_disorder_id]);
unset($data[$assignment->ophcocvi_clinicinfo_disorder_id]);
} else {
if (!$assignment->delete()) {
throw new \Exception('Unable to delete CVI Disorder Assignment: ' . print_r($assignment->getErrors(), true));
}
}
}
// create new assignments that don't yet exist for the element
foreach ($data as $cvi_disorder_id => $values) {
$assignment = new Element_OphCoCvi_ClinicalInfo_Disorder_Assignment();
$assignment->eye_id = $eye_id;
$assignment->ophcocvi_clinicinfo_disorder_id = $cvi_disorder_id;
$this->updateDisorderAssignment($assignment, $values);
}
}