public function updateReadings($side, $readings)
{
$curr_by_id = array();
$save = array();
foreach ($this->readings as $r) {
if ($r->eye_id == $side) {
$curr_by_id[$r->method->id] = $r;
}
}
foreach ($readings as $reading) {
if (!($method_id = $reading['method_id'])) {
$method_id = OphCiExamination_ColourVision_Value::model()->findByPk($reading['value_id'])->method->id;
}
if (!array_key_exists($method_id, $curr_by_id)) {
$obj = new OphCiExamination_ColourVision_Reading();
} else {
$obj = $curr_by_id[$method_id];
unset($curr_by_id[$method_id]);
}
$obj->attributes = $reading;
$obj->element_id = $this->id;
$obj->eye_id = $side;
$save[] = $obj;
}
foreach ($save as $s) {
if (!$s->save()) {
throw new \Exception('unable to save reading:' . print_r($s->getErrors(), true));
}
}
foreach ($curr_by_id as $curr) {
if (!$curr->delete()) {
throw new \Exception('unable to delete reading:' . print_r($curr->getErrors(), true));
}
}
}