/** * Creates and set default template for given $this->formModel. * * @throws Exception database exception if creating fails * @return Template instance or it's descedant */ private function createDefaultTemplate() { $class = get_called_class(); $template; switch ($class::TEMPLATE_TYPE) { case $class::TEMPLATE_FORM: $template = new FlexformTemplate(); break; case $class::TEMPLATE_GRID: $template = new FlexgridTemplate(); break; } $template->name = 'Default'; $template->public = 1; $template->type = $class::TEMPLATE_TYPE; $template->FK_user = \Yii::$app->user->identity->id; $template->modelClass = $this->template_name; $template->current = 1; if (!$template->save()) { throw new Exception('Database error during creating deafault template.\\n' . print_r($template->getErrors(), true)); } $template->refresh(); $template->setCurrent(); foreach ($this->getDisplayedAttributes() as $key => $value) { $tempField = new FlexformField(); $tempField->FK_template = $template->id; $tempField->name = $value; $tempField->order = $key; $tempField->save(); } return $this->template = $template; }