/** * Delete the queue and the queues that are are solely dependent on it. * * @param $queue_id * * @throws \Exception * @throws \Exception */ public function delete($queue_id) { $transaction = Yii::app()->db->getCurrentTransaction() === null ? Yii::app()->db->beginTransaction() : false; try { $queue = $this->readModel($queue_id); // remove dependendent outcomes $remove_ids = $queue->getDependentQueueIds(); $remove_ids[] = $queue_id; // how I'd do it if BaseActiveRecordVersioned supported delete with an in condition /* $criteria = new \CDbCriteria(); $criteria->addInCondition('outcome_queue_id', $remove_ids); $criteria->addInCondition('queue_id', $remove_ids, 'OR'); models\QueueOutcome::model()->deleteAll($criteria); // remove dependent and actual queues $criteria = new \CDbCriteria(); $criteria->addInCondition($this->model->getPrimaryKey(), $remove_ids); $this->model->deleteAll($criteria); */ // instead ... foreach ($remove_ids as $rid) { $criteria = new \CDbCriteria(); $criteria->addColumnCondition(array('outcome_queue_id' => $rid, 'queue_id' => $rid), 'OR'); models\QueueOutcome::model()->deleteAll($criteria); $this->model->deleteByPk($rid); } \Audit::add('admin', 'delete', $queue->id, null, array('module' => 'PatientTicketing', 'model' => $queue->getShortModelName())); if ($transaction) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction) { $transaction->rollback(); } throw $e; } }
/** * Performs the update/create process on a Queue. * * @param $queue * @param null $parent * * @throws \CHttpException */ protected function saveQueue($queue, $parent = null) { // try and process form $queue->attributes = $_POST; if (!$parent && $queue->isNewRecord) { $queue->is_initial = true; } if (!$queue->validate()) { $resp = array('success' => false, 'form' => $this->renderPartial('form_queue', array('parent' => $parent, 'queue' => $queue, 'errors' => $queue->getErrors()), true)); echo \CJSON::encode($resp); } else { $transaction = Yii::app()->db->beginTransaction(); try { $action = $queue->isNewRecord ? 'create' : 'update'; $queue->save(); if ($parent) { $outcome = new models\QueueOutcome(); $outcome->queue_id = $parent->id; $outcome->outcome_queue_id = $queue->id; $outcome->save(); } \Audit::add('admin', $action, $queue->id, null, array('module' => 'PatientTicketing', 'model' => $queue->getShortModelName())); $transaction->commit(); $resp = array('success' => true, 'queueId' => $queue->id); echo \CJSON::encode($resp); } catch (Exception $e) { $transaction->rollback(); throw new \CHttpException(500, 'Unable to create queue'); } } }