试图在 yii2 中获取非对象的属性

2022-01-07 00:00:00 php yii2 yii2-model

我想访问 hasMany 关系,但此内容出错

<块引用>

PHP 通知 – yiiaseErrorException

试图获取非对象的属性

这是我的观点:news/index.php

<!-- 项目一--><div class="row"><div class="col-md-2"><a href="#"><img class="img-responsive" src="photos/<?=$model->photos->id?>.jpg" alt=""></a>

<div class="col-md-10"><h4><div class="row"><div class="col-sm-8">دسته بندی:<?= $model->cat->name;?>

<div class="col-sm-4">تاریخ:

<h3><?=$model->title?></h3><p><?=$model->body ?></p><a class="btn btn-primary" href="#">查看项目 <span class="glyphicon glyphicon-chevron-right"></span></a>

<小时><!--/.row --><?php endforeach;?>

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

 公共函数 getCat(){返回 $this->hasOne(Categories::className(), ['id' => 'cat_id']);}/*** @return yiidbActiveQuery*/公共函数 getPhotos(){返回 $this->hasMany(Photos::className(), ['news_id' => 'id']);}/*** @return yiidbActiveQuery*/公共函数 getViews(){返回 $this->hasMany(Views::className(), ['news_id' => 'id']);}/*** @return yiidbActiveQuery*/公共函数 getVotes(){返回 $this->hasMany(Votes::className(), ['news_id' => 'id']);}

这是新闻控制器

 all();返回 $this->render('index',compact('models'));}}

这是vardump($model->photos)

 数组(1) {[0]=>对象(前端模型照片)#84 (8) {["_attributes":"yiidbBaseActiveRecord":private]=>数组(2){[id"]=>整数(2)["news_id"]=>整数(2)}["_oldAttributes":"yiidbBaseActiveRecord":private]=>数组(2){[id"]=>整数(2)["news_id"]=>整数(2)}["_related":"yiidbBaseActiveRecord":private]=>数组(0){}["_errors":"yiiaseModel":private]=>空值["_validators":"yiiaseModel":private]=>空值["_scenario":"yiiaseModel":private]=>字符串(7)默认"["_events":"yiiaseComponent":private]=>数组(0){}["_behaviors":"yiiaseComponent":private]=>数组(0){}}}

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

解决方案

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

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

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

PHP Notice – yiiaseErrorException

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 frontendmodelsNews relations

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

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

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

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

and this is NewsController

    <?php

namespace frontendcontrollers;
use frontendmodelsNews;

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

}

and this is vardump($model->photos)

         array(1) {
  [0]=>
  object(frontendmodelsPhotos)#84 (8) {
    ["_attributes":"yiidbBaseActiveRecord":private]=>
    array(2) {
      ["id"]=>
      int(2)
      ["news_id"]=>
      int(2)
    }
    ["_oldAttributes":"yiidbBaseActiveRecord":private]=>
    array(2) {
      ["id"]=>
      int(2)
      ["news_id"]=>
      int(2)
    }
    ["_related":"yiidbBaseActiveRecord":private]=>
    array(0) {
    }
    ["_errors":"yiiaseModel":private]=>
    NULL
    ["_validators":"yiiaseModel":private]=>
    NULL
    ["_scenario":"yiiaseModel":private]=>
    string(7) "default"
    ["_events":"yiiaseComponent":private]=>
    array(0) {
    }
    ["_behaviors":"yiiaseComponent":private]=>
    array(0) {
    }
  }
}

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

解决方案

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)

相关文章