Element_OphTrOperationnote_PostOpDrugs::updateDrugs PHP Метод

updateDrugs() публичный Метод

public updateDrugs ( $drug_ids )
    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));
            }
        }
    }

Usage Example

Пример #1
0
 /**
  * Update the drug assignments.
  *
  * @param Element_OphTrOperationnote_PostOpDrugs $element
  * @param $data
  * @param $index
  */
 protected function saveComplexAttributes_Element_OphTrOperationnote_PostOpDrugs($element, $data, $index)
 {
     $element->updateDrugs(isset($data['Drug']) ? $data['Drug'] : array());
 }