title: Trying to get property of non-object in yii2
body: <p>I want access to hasMany relation but i get error with this content </p>
  
  <blockquote>
    <p>PHP Notice – yii\base\ErrorException</p>
    
    <p>Trying to get property of non-object</p>
  </blockquote>
  
  <p>this is my view :  news/index.php </p>
  
  <pre><code>&lt;?php foreach($models as $model): ?&gt;
  &lt;!-- Project One --&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-2"&gt;
          &lt;a href="#"&gt;
              &lt;img class="img-responsive" src="photos/&lt;?=$model-&gt;photos-&gt;id?&gt;.jpg" alt=""&gt;
          &lt;/a&gt;
      &lt;/div&gt;
      &lt;div class="col-md-10"&gt;
          &lt;h4&gt;
              &lt;div class="row"&gt;
                  &lt;div class="col-sm-8"&gt;
                      دسته بندی:&lt;?= $model-&gt;cat-&gt;name; ?&gt;
                  &lt;/div&gt;
                  &lt;div class="col-sm-4"&gt;
                      تاریخ:
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/h4&gt;
          &lt;h3&gt;&lt;?=$model-&gt;title ?&gt;&lt;/h3&gt;
          &lt;p&gt;&lt;?=$model-&gt;body ?&gt;&lt;/p&gt;
          &lt;a class="btn btn-primary" href="#"&gt;View Project &lt;span class="glyphicon glyphicon-chevron-right"&gt;&lt;/span&gt;&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
          &lt;hr&gt;
  &lt;!-- /.row --&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>and this is my frontend\models\News relations</p>
  
  <pre><code>  public function getCat()
  {
      return $this-&gt;hasOne(Categories::className(), ['id' =&gt; 'cat_id']);
  }
  
  /**
   * @return \yii\db\ActiveQuery
   */
  public function getPhotos()
  {
      return $this-&gt;hasMany(Photos::className(), ['news_id' =&gt; 'id']);
  }
  
  /**
   * @return \yii\db\ActiveQuery
   */
  public function getViews()
  {
      return $this-&gt;hasMany(Views::className(), ['news_id' =&gt; 'id']);
  }
  
  /**
   * @return \yii\db\ActiveQuery
   */
  public function getVotes()
  {
      return $this-&gt;hasMany(Votes::className(), ['news_id' =&gt; 'id']);
  }
  </code></pre>
  
  <p>and this is NewsController</p>
  
  <pre><code>    &lt;?php
  
  namespace frontend\controllers;
  use frontend\models\News;
  
  class NewsController extends \yii\web\Controller
  {
      public function actionIndex()
      {
          $models =News::find()-&gt;all();
          return $this-&gt;render('index',compact('models'));
      }
  
  }
  </code></pre>
  
  <p>and this is <code>vardump($model-&gt;photos)</code></p>
  
  <pre><code>         array(1) {
    [0]=&gt;
    object(frontend\models\Photos)#84 (8) {
      ["_attributes":"yii\db\BaseActiveRecord":private]=&gt;
      array(2) {
        ["id"]=&gt;
        int(2)
        ["news_id"]=&gt;
        int(2)
      }
      ["_oldAttributes":"yii\db\BaseActiveRecord":private]=&gt;
      array(2) {
        ["id"]=&gt;
        int(2)
        ["news_id"]=&gt;
        int(2)
      }
      ["_related":"yii\db\BaseActiveRecord":private]=&gt;
      array(0) {
      }
      ["_errors":"yii\base\Model":private]=&gt;
      NULL
      ["_validators":"yii\base\Model":private]=&gt;
      NULL
      ["_scenario":"yii\base\Model":private]=&gt;
      string(7) "default"
      ["_events":"yii\base\Component":private]=&gt;
      array(0) {
      }
      ["_behaviors":"yii\base\Component":private]=&gt;
      array(0) {
      }
    }
  }
  </code></pre>
  
  <p>i can access to <code>$model-&gt;cat-&gt;name</code> but i can't access to <code>$model-&gt;photos-&gt;id</code>  why?!</p>