Element_OphTrOperationnote_Anaesthetic::updateAnaestheticAgents PHP Method

updateAnaestheticAgents() public method

Update the Anaesthetic Agents associated with the element.
public updateAnaestheticAgents ( $agent_ids )
$agent_ids
    public function updateAnaestheticAgents($agent_ids)
    {
        $curr_by_id = array();
        foreach ($this->anaesthetic_agent_assignments as $aa) {
            $curr_by_id[$aa->anaesthetic_agent_id] = $aa;
        }
        if (!empty($agent_ids)) {
            foreach ($agent_ids as $aa_id) {
                if (!isset($curr_by_id[$aa_id])) {
                    $aa = new OphTrOperationnote_OperationAnaestheticAgent();
                    $aa->et_ophtroperationnote_anaesthetic_id = $this->id;
                    $aa->anaesthetic_agent_id = $aa_id;
                    if (!$aa->save()) {
                        throw new Exception('Unable to save anaesthetic agent assignment: ' . print_r($aa->getErrors(), true));
                    }
                } else {
                    unset($curr_by_id[$aa_id]);
                }
            }
        }
        foreach ($curr_by_id as $aa) {
            if (!$aa->delete()) {
                throw new Exception('Unable to delete anaesthetic agent assignment: ' . print_r($aa->getErrors(), true));
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Update the anaesthetic agents and complications.
  *
  * @param Element_OphTrOperationnote_Anaesthetic $element
  * @param $data
  * @param $index
  */
 protected function saveComplexAttributes_Element_OphTrOperationnote_Anaesthetic($element, $data, $index)
 {
     $element->updateAnaestheticAgents(isset($data['AnaestheticAgent']) ? $data['AnaestheticAgent'] : array());
     $element->updateComplications(isset($data['OphTrOperationnote_AnaestheticComplications']) ? $data['OphTrOperationnote_AnaestheticComplications'] : array());
 }