title: before save in Yii2
body: <p>I have a form with multi-select fields named "city" form in Yii2. When I submit form
  the post data show me the following:</p>
  
  <pre><code>$_POST['city'] = array('0'=&gt;'City A','1'=&gt;'City B','2'=&gt;'City C')
  </code></pre>
  
  <p>But I want to save the array in serialize form  like:</p>
  
  <pre><code>a:3:{i:0;s:6:"City A";i:1;s:6:"City B";i:2;s:6:"City C";}
  </code></pre>
  
  <p>But I don't know how to modify data before save function in Yii2. Followin is my code:</p>
  
  <pre><code>if(Yii::$app-&gt;request-&gt;post()){
  
      $_POST['Adpackage']['Page'] = serialize($_POST['Adpackage']['Page']);  
      $_POST['Adpackage']['fixer_type'] = serialize($_POST['Adpackage']['fixer_type']);  
  }
  
   if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;save()) {
  
         return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id]);
    } else {
  
        return $this-&gt;render('create', [
              'model' =&gt; $model   
          ]);
    }
  </code></pre>
  
  <p>Please help me. </p>
  
  <p>Thanks all for your effort. I have solved the issue. here is the code :</p>
  
  <pre><code>public function beforeSave($insert)
      {
          if (parent::beforeSave($insert)) {
              $this-&gt;Page = serialize($_POST['Adpackage']['Page']);
              $this-&gt;fixer_type = serialize($_POST['Adpackage']['fixer_type']);
              return true;
          } else {
              return false;
          }
      }
  </code></pre>
  
  <p>Just put this code in model and its working</p>