title: Yii2: Assign sum of textfields to another field in form
body: <p>Im new to Yii and I'm trying to assign the sum of 3 different fields in my _form.php into a total field. I've tried assigning it directly in the controller but the total field stays null. I'm basically trying to achieve this:  </p>
  
  <pre><code>$model-&gt;cuerdas= $model-&gt;propia + $model-&gt;usofructo + $model-&gt;arrendada;
  </code></pre>
  
  <p>This is the actionCreate code of the Controller where I tried making the assignment:</p>
  
  <pre><code> public function actionCreate()
  {
      $model = new AsdaPa0025();
      $modelosfacilidades = [new FacilidadesARealizar0025()];
  
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post())) {
          $model-&gt;file = UploadedFile::getInstance($model, 'file');
          $modelosfacilidades = Model::createMultiple(FacilidadesARealizar0025::classname());
          Model::loadMultiple($modelosfacilidades, Yii::$app-&gt;request-&gt;post());
  
          //valida los modelos
          $valid = $model-&gt;validate();
          $valid = Model::validateMultiple($modelosfacilidades) &amp;&amp; $valid;
  
          if ($valid) {
              $transaction = \Yii::$app-&gt;db-&gt;beginTransaction();
              try {
                  if ($flag = $model-&gt;save(false)) {
                      foreach ($modelosfacilidades as $modelofacilidades) {
                          //Aqui le digo al controlador que id_0025 es igual al id de la instancia de la forma 0025
                          $modelofacilidades-&gt;id_0025 = $model-&gt;id_asda_pa_0025;
                          if (! ($flag = $modelofacilidades-&gt;save(false))) {
                              $transaction-&gt;rollBack();
                              break;
                          }
                      }
                  }
                  if ($flag) {
                      $transaction-&gt;commit();
                      $model-&gt;cuerdas= $model-&gt;propia + $model-&gt;usofructo + $model-&gt;arrendada;
                      $model-&gt;file-&gt;saveAs('uploads/' . $model-&gt;file-&gt;baseName . '.' . $model-&gt;file-&gt;extension);
                      return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id_asda_pa_0025]);
                  }
              } catch (Exception $e) {
                  $transaction-&gt;rollBack();
              }
          }
  
         // $model-&gt;file = 'uploads/' . $model-&gt;imageFile-&gt;baseName . '.' . $model-&gt;imageFile-&gt;extension;
  
         // if ($model-&gt;save()) {
          //    $model-&gt;file-&gt;saveAs('uploads/' . $model-&gt;file-&gt;baseName . '.' . $model-&gt;file-&gt;extension);
            //  return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id_asda_pa_0025]);
       }
       else{
        // return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id_asda_pa_0025]);
  
        return $this-&gt;render('create', [
          'model' =&gt; $model,
          'modelosfacilidades' =&gt; (empty($modelosfacilidades)) ? [new FacilidadesARealizar0025] : $modelosfacilidades
  
      ]);
  
       }
  
  
     }
  </code></pre>
  
  <p>I also tried calling this function from model in the controller instead of the manual assignment but I get a <strong>using this->() when not in object context error</strong></p>
  
  <pre><code>/**
   * @return \yii\db\ActiveQuery
   */
  public static function getSuma()
  {
  
    return $this-&gt;find()-&gt;sum('propia + arrendada + usofructo');
  
  
  }
  </code></pre>
  
  <p>This is my _form.php where the fields are:</p>
  
  <pre><code>&lt;div class="col-sm-6"&gt;
           Cuerdas
           &lt;?= $form-&gt;field($model, 'propia')-&gt;textInput()-&gt;label('7.Propia') ?&gt;
           &lt;?= $form-&gt;field($model, 'arrendada')-&gt;textInput()-&gt;label('8.Arrendada') ?&gt;
           &lt;?= $form-&gt;field($model, 'usofructo')-&gt;textInput()-&gt;label('9.Usufructo') ?&gt;
           &lt;?= $form-&gt;field($model, 'cuerdas')-&gt;textInput(['readOnly'=&gt; true])-&gt;label('10. Total Cuerdas')?&gt;
  
  
      &lt;/div&gt;
  </code></pre>
  
  <p>I turned the cuerdas field input as readonly since it is going to be the one that will receive the sum of the other 3 values. </p>
  
  <p>I would appreciate any help on how would the correct approach be to achieve this.</p>