/**
  * Creates a new CommentsFields model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new CommentsFields();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'comments_id' => $model->comments_id, 'field_name' => $model->field_name]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function save($runValidation = true, $attributeNames = null)
 {
     $isNewRecord = $this->isNewRecord;
     $transaction = \Yii::$app->db->beginTransaction();
     if (!parent::save($runValidation, parent::attributes())) {
         $transaction->rollback();
         return false;
     }
     if ($isNewRecord) {
         $commentLink = new CommentsLink();
         $commentLink->comments_id = $this->id;
         $commentLink->url = $this->url;
         $commentLink->link_params = $this->link_params;
         $commentLink->user_id = !\Yii::$app->user->isGuest ? \Yii::$app->user->id : null;
         if (!$commentLink->save()) {
             $transaction->rollback();
             return false;
         }
     } else {
         CommentsFields::deleteAll(['comments_id' => $this->id]);
     }
     foreach ($this->fields as $field) {
         $name = $field['name'];
         if ($this->{$name} != null) {
             $model = new CommentsFields();
             $model->comments_id = $this->id;
             $model->field_name = $field['name'];
             $model->value = $this->{$name};
             if (!($st = $model->save($runValidation = true))) {
                 $transaction->rollback();
                 return false;
             }
         }
     }
     $transaction->commit();
     return true;
 }