试图在yii2

时间:2015-10-27 11:56:39

标签: php yii2 yii2-model

我想访问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; ?>

这是我的前端\ 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']);
}

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

}

这是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为什么?!

3 个答案:

答案 0 :(得分:1)

您遇到的问题是$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="">';
}

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

答案 1 :(得分:0)

您可以访问,因为它具有 hasMany 关系而$model->photos ActiveQuery ,因此您可以使用{{1对于第一个或使用$model->photos->one()->id

进行循环

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

答案 2 :(得分: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>