public function updateDrugs($drug_ids)
{
$curr_by_id = array();
foreach (OphTrOperationnote_OperationDrug::model()->findAll('ophtroperationnote_postop_drugs_id = :drugsId', array(':drugsId' => $this->id)) as $od) {
$curr_by_id[$od->drug_id] = $od;
}
if (!empty($drug_ids)) {
foreach ($drug_ids as $d_id) {
if (!isset($curr_by_id[$d_id])) {
$da = new OphTrOperationnote_OperationDrug();
$da->ophtroperationnote_postop_drugs_id = $this->id;
$da->drug_id = $d_id;
if (!$da->save()) {
throw new Exception('Unable to save drug assignment: ' . print_r($da->getErrors(), true));
}
} else {
unset($curr_by_id[$d_id]);
}
}
}
foreach ($curr_by_id as $curr) {
if (!$curr->delete()) {
throw new Exception('Unable to delete drug assignment: ' . print_r($curr->getErrors(), true));
}
}
}