Yii2 Invalid Call: Setting read-only property
<p>I have a <code>Post</code> model that has a many-many relationship with <code>Tags</code>. </p>

<p>Defined in Post model:</p>

<pre><code>public function getTags(){
    return $this-&gt;hasMany(Tags::className(), ['id' =&gt; 'tag_id'])
        -&gt;viaTable('post_tags', ['post_id' =&gt; 'id']);
}
</code></pre>

<p>But <code>Post::tags</code> is read-only. So when I try to set them in the Controller, I get an error:</p>

<blockquote>
  <p>Invalid Call – yii\base\InvalidCallException</p>
  
  <p>Setting read-only property: app\models\Post::tags</p>
</blockquote>

<p>The controller is using load to set all the properties:</p>

<pre><code>public function actionCreate(){
    $P = new Post();
    if( Yii::$app-&gt;request-&gt;post() ){
        $P-&gt;load(Yii::$app-&gt;request-&gt;post());
        $P-&gt;save();
        return $this-&gt;redirect('/posts');
    }
    return $this-&gt;render('create', ['model'=&gt;$P]);
}
</code></pre>

<p>The input field in the view:</p>

<pre><code>&lt;?= $form-&gt;field($model, 'tags')-&gt;textInput(['value'=&gt;$model-&gt;stringTags()]) ?&gt;
</code></pre>

<p>Why is <code>Post::tags</code> read-only? And whats the proper way to set a model relationship?</p>