问题描述
我想访问hasMany关系,但是此内容出现错误
I want access to hasMany relation but i get error with this content
试图获取非对象的属性
这是我的观点:news/index.php
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']);
}
这是NewsController
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'));
}
}
这是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
?
i can access to $model->cat->name
but i can't access to $model->photos->id
why?!
推荐答案
您遇到的问题是$model->photos
是一个数组.这是因为您使用hasMany()
设置了关系,这意味着每个News
可能具有多个Photos
.
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="">
这将显示第一张照片(假设有一张,如果没有更改,则您必须检查是否已设置0
).
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)
这篇关于试图获取yii2中非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!