public function actionAddworkflowStep() { if (!($workflow = models\OphCiExamination_Workflow::model()->findByPk(@$_POST['workflow_id']))) { throw new \Exception('Workflow not found: ' . @$_POST['workflow_id']); } if ($current_last = models\OphCiExamination_ElementSet::model()->find(array('condition' => 'workflow_id = :workflow_id', 'params' => array(':workflow_id' => $workflow->id), 'order' => 'position desc'))) { $current_last_position = $current_last->position; } else { $current_last_position = 0; } $set = new models\OphCiExamination_ElementSet(); $set->workflow_id = $workflow->id; $set->position = $current_last_position + 1; $set->name = 'Step ' . $set->position; if (!$set->save()) { throw new \Exception('Unable to save element set: ' . print_r($set->getErrors(), true)); } echo json_encode(array('id' => $set->id, 'position' => $set->position, 'name' => $set->name)); }