Trying to get property of non-object in yii2

majid  picture majid · Oct 27, 2015 · Viewed 22k times · Source

I want access to hasMany relation but i get error with this content

PHP Notice – yii\base\ErrorException

Trying to get property of non-object

this is my view : news/index.php

<?php foreach($models as $model): ?>
<!-- Project One -->
<div class="row">
    <div class="col-md-2">
        <a href="#">
            <img class="img-responsive" src="photos/<?=$model->photos->id?>.jpg" alt="">
        </a>
    </div>
    <div class="col-md-10">
        <h4>
            <div class="row">
                <div class="col-sm-8">
                    دسته بندی:<?= $model->cat->name; ?>
                </div>
                <div class="col-sm-4">
                    تاریخ:
                </div>
            </div>
        </h4>
        <h3><?=$model->title ?></h3>
        <p><?=$model->body ?></p>
        <a class="btn btn-primary" href="#">View Project <span class="glyphicon glyphicon-chevron-right"></span></a>
    </div>
</div>
        <hr>
<!-- /.row -->
<?php endforeach; ?>

and this is my frontend\models\News relations

  public function getCat()
{
    return $this->hasOne(Categories::className(), ['id' => 'cat_id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getPhotos()
{
    return $this->hasMany(Photos::className(), ['news_id' => 'id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getViews()
{
    return $this->hasMany(Views::className(), ['news_id' => 'id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getVotes()
{
    return $this->hasMany(Votes::className(), ['news_id' => 'id']);
}

and this is NewsController

    <?php

namespace frontend\controllers;
use frontend\models\News;

class NewsController extends \yii\web\Controller
{
    public function actionIndex()
    {
        $models =News::find()->all();
        return $this->render('index',compact('models'));
    }

}

and this is vardump($model->photos)

         array(1) {
  [0]=>
  object(frontend\models\Photos)#84 (8) {
    ["_attributes":"yii\db\BaseActiveRecord":private]=>
    array(2) {
      ["id"]=>
      int(2)
      ["news_id"]=>
      int(2)
    }
    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
    array(2) {
      ["id"]=>
      int(2)
      ["news_id"]=>
      int(2)
    }
    ["_related":"yii\db\BaseActiveRecord":private]=>
    array(0) {
    }
    ["_errors":"yii\base\Model":private]=>
    NULL
    ["_validators":"yii\base\Model":private]=>
    NULL
    ["_scenario":"yii\base\Model":private]=>
    string(7) "default"
    ["_events":"yii\base\Component":private]=>
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=>
    array(0) {
    }
  }
}

i can access to $model->cat->name but i can't access to $model->photos->id why?!

Answer

D.Mill picture D.Mill · Oct 27, 2015

The problem you're having is that $model->photos is an array. This is because you set the relationship with hasMany() implying that each News could have multiple Photos.

You can either do the following in your view:

<img class="img-responsive" src="photos/<?=$model->photos[0]->id?>.jpg" alt="">

This will display the first photo (provided there is one, if there's a change there are none then you would have to check that 0 is set).

Or you can display all photos with:

foreach($model->photos as $photo)
{ 
   echo '<img class="img-responsive" src="photos/'.$photo->id.'.jpg" alt="">';
}

(or any other design you want)