title: Output relational data in Yii2
body: <p>I trying to output related data in Yii2. My output code looks like this(like i do that in Yii 1):</p>
  
  <pre><code>foreach ($model-&gt;comments as $key =&gt; $comment) {
      echo $comment-&gt;title;
  }
  </code></pre>
  
  <p>But i get the error <code>tying to get property on a non-object</code>, var_dump of <code>$model-&gt;comments</code> show the array, what look like:</p>
  
  <pre><code>    array (size=1)
    0 =&gt; 
      object(common\models\Comment)[74]
        private '_attributes' (yii\db\BaseActiveRecord) =&gt; 
          array (size=5)
            'id' =&gt; int 1
            'title' =&gt; string 'testComment' (length=11)
            'body' =&gt; string 'body' (length=4)
            'post_id' =&gt; int 1
            'user_id' =&gt; int 1
        private '_oldAttributes' (yii\db\BaseActiveRecord) =&gt; 
          array (size=5)
            'id' =&gt; int 1
            'title' =&gt; string 'testComment' (length=11)
            'body' =&gt; string 'body' (length=4)
            'post_id' =&gt; int 1
            'user_id' =&gt; int 1
        private '_related' (yii\db\BaseActiveRecord) =&gt; 
          array (size=0)
            empty
  </code></pre>
  
  <p>var_dump of <code>$model-&gt;getComments()</code> return the ActiveQuery object:</p>
  
  <pre><code>object(yii\db\ActiveQuery)[67]
    public 'sql' =&gt; null
    public 'on' =&gt; null
    public 'joinWith' =&gt; null
    public 'select' =&gt; null
    public 'selectOption' =&gt; null
    public 'distinct' =&gt; null
    public 'from' =&gt; null
    public 'groupBy' =&gt; null
    public 'join' =&gt; null
    public 'having' =&gt; null
    public 'union' =&gt; null
    public 'params' =&gt; 
      array (size=0)
        empty
    private '_events' (yii\base\Component) =&gt; 
      array (size=0)
        empty
    private '_behaviors' (yii\base\Component) =&gt; 
      array (size=0)
        empty
    public 'where' =&gt; null
    public 'limit' =&gt; null
    public 'offset' =&gt; null
    public 'orderBy' =&gt; null
    public 'indexBy' =&gt; null
    public 'modelClass' =&gt; string 'common\models\Comment' (length=21)
    public 'with' =&gt; null
    public 'asArray' =&gt; null
    public 'multiple' =&gt; boolean true
    public 'primaryModel' =&gt; 
      object(common\models\Post)[65]
        private '_attributes' (yii\db\BaseActiveRecord) =&gt; 
          array (size=5)
            'id' =&gt; int 1
            'header' =&gt; string 'ds' (length=2)
            'content' =&gt; string 'dsad' (length=4)
            'created' =&gt; string '0000-00-00' (length=10)
            'status' =&gt; int 1
        private '_oldAttributes' (yii\db\BaseActiveRecord) =&gt; 
          array (size=5)
            'id' =&gt; int 1
            'header' =&gt; string 'ds' (length=2)
            'content' =&gt; string 'dsad' (length=4)
            'created' =&gt; string '0000-00-00' (length=10)
            'status' =&gt; int 1
        private '_related' (yii\db\BaseActiveRecord) =&gt; 
          array (size=0)
            empty
        private '_errors' (yii\base\Model) =&gt; null
        private '_validators' (yii\base\Model) =&gt; null
        private '_scenario' (yii\base\Model) =&gt; string 'default' (length=7)
        private '_events' (yii\base\Component) =&gt; 
          array (size=0)
            empty
        private '_behaviors' (yii\base\Component) =&gt; 
          array (size=0)
            empty
    public 'link' =&gt; 
      array (size=1)
        'post_id' =&gt; string 'id' (length=2)
    public 'via' =&gt; null
    public 'inverseOf' =&gt; null
  </code></pre>
  
  <p>I see my data in both cases, but how i can recieve them from this constructs?  (and what way $model->getComments() or $model->comments is wright?)</p>