public function configure($array)
 {
     FlexformField::deleteAll("FK_template = {$this->id} AND name NOT IN ( '" . implode('\', \'', $array) . "' )");
     foreach ($array as $key => $value) {
         if (FlexformField::find()->where(['FK_template' => $this->id, 'name' => $value])->one() == null) {
             $model = new FlexformField();
             $model->name = $value;
             $model->order = $key;
             $model->FK_template = $this->id;
             $model->save();
         }
     }
 }
 public function actionAddField()
 {
     if (isset($_POST['templateId']) && isset($_POST['fieldName'])) {
         $field = new FlexformField();
         $field->name = $_POST['fieldName'];
         $field->FK_template = $_POST['templateId'];
         $field->order = 1;
         if (!$field->save()) {
             throw new Exception('Error occured during creating field.\\n' . print_r($field->getErrors(), true));
         }
     }
     return 'field' . $_POST['fieldName'] . ' to template ' . $_POST['templateId'] . ' added';
 }
 /**
  * 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;
 }