title: How to save user password in Hash format into DB in Yii2
body: <p>I need to create new user. And I want to save password into hash format in DB. But I failed several times. </p>
  
  <p>Here is my code:</p>
  
  <h1>Controller</h1>
  
  <pre><code>public function actionCreate()
  {
      $model = new User();
  
      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>
  
  <h1>User Model</h1>
  
  <pre><code>public function validatePassword($password)
  {
      return Security::validatePassword($password, $this-&gt;password_hash);
  }
  
  
  /**
   * Generates password hash from password and sets it to the model
   *
   * @param string $password
   */
  
  
  public function setPassword($password)
  {
      $this-&gt;password_hash = Security::generatePasswordHash($password);
  }
  </code></pre>
  
  <h1>User Table Rules</h1>
  
  <pre><code>public function rules()
  {
      return [
          [['c_authkey', 'inserttime'], 'required'],
          [['c_branch', 'c_roleid', 'c_active', 'c_status'], 'integer'],
          [['c_expdate', 'inserttime', 'updatetime'], 'safe'],
          [['username', 'c_desig', 'insertuser', 'updateuser'], 'string', 'max' =&gt; 50],
          [['password'], 'string', 'max' =&gt; 32],
          [['c_authkey'], 'string', 'max' =&gt; 256],
          [['c_name'], 'string', 'max' =&gt; 100],
          [['c_cellno'], 'string', 'max' =&gt; 15],
          [['username'], 'unique']
      ];
  }
  </code></pre>
  
  <p>What I am missing? what's the solution?</p>