4

我想访问 hasMany 关系,但我收到此内容错误

PHP 通知 – yii\base\ErrorException

试图获取非对象的属性

这是我的观点: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; ?>

这是我的前端\模型\新闻关系

  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']);
}

这是新闻控制器

    <?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'));
    }

}

这是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) {
    }
  }
}

我可以访问$model->cat->name但我无法访问$model->photos->id 为什么?!

4

3 回答 3

3

您遇到的问题是这$model->photos是一个数组。这是因为您将关系设置为hasMany()暗示每个News可以有多个Photos.

您可以在视图中执行以下操作:

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

这将显示第一张照片(前提是有一张,如果有更改则没有,那么您必须检查是否0已设置)。

或者您可以显示所有照片:

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

(或您想要的任何其他设计)

于 2015-10-27T13:14:37.007 回答
0

您可以访问,因为它具有hasMany关系并且$model->photosActiveQuery,因此您可以使用$model->photos->one()->id第一个或使用循环$model->photos->all()

更多信息http ://www.yiiframework.com/doc-2.0/yii-db-activequery.html

于 2015-10-27T12:11:28.923 回答
0

您总是可以稍后检查第一次获取,这样您将添加一个小的延迟,但您会隐藏消息并检查输出,即

    if ($model->photos)
    { return $model->photos->id;}
    else 
    {return NULL;} //or whatever you want
// or in your case 
    <div class="col-md-2">
        <a href="#">
    <?php (if $model->photos) 
        { 
   echo   
    '<img class="img-responsive" src="photos/'.$model->photos->id'.jpg"alt="">';
     } 
    else 
    {   
     echo   
    '<img class="img-responsive" src="photos/my-default_photo_for_no_photo.jpg" alt=""'; //or whatever else 
     }
    ?>
    </a>
        </div>
于 2016-12-07T21:59:57.760 回答