我的控制器中有这个功能:
public function actionResource()
{
$resourcegrporgs = Organization::find()->where(['groupid' => '1'])->all();
$resmembers = '';
foreach ($resourcegrporgs as $resourcegrporg) {
$resmembers = $resourcegrporg->getMembers()->all();
}
return $this->render('resource', [
'resourceMembers' => $resmembers,
'resourceGroups' => $resourcegrporgs,
]);
}
在我看来,我试着让每个小组的成员如下:
<?php foreach ($resourceGroups as $resourceGroup): ?>
<section class="team">
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="col-lg-12">
<h4 class="description"><?= $resourceGroup->name ?></h4>
<div class="row pt-md">
<?php foreach ($resourceMembers as $resourceMember): ?>
<div class="col-lg-3 col-md-3 col-sm-4 col-xs-12 profile">
<div class="img-box">
<img src=<?= $resourceMember->picture ?> class="img-responsive">
<ul class="text-center">
<a href="#"><li><i class="fa fa-facebook"></i></li></a>
<a href="#"><li><i class="fa fa-twitter"></i></li></a>
<a href="#"><li><i class="fa fa-linkedin"></i></li></a>
</ul>
</div>
<h1><?= Html::a(Html::encode($resourceMember->name),['memberdetail', 'id' => $resourceMember->id]) ?></h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
</section>
<?php endforeach; ?>
这是组织模式;
class Organization extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'organization';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'active', 'groupid'], 'required'],
[['active', 'groupid'], 'integer'],
[['lastdatemodified'], 'safe'],
[['name', 'logo', 'userid'], 'string', 'max' => 100]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'logo' => 'Logo',
'active' => 'Active',
'userid' => 'Userid',
'lastdatemodified' => 'Lastdatemodified',
'groupid' => 'Groupid',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getMembers()
{
return $this->hasMany(Member::className(), ['organizationid' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getGroup()
{
return $this->hasOne(Group::className(), ['id' => 'groupid']);
}
}
但是,成员的foreach
循环返回每个其他组的最后一个组的成员,因此我的问题是:
如何修复它以单独呈现每个组的成员?
答案 0 :(得分:1)
除非我遗漏了一些东西,否则这很容易。只需将您的操作更改为以下内容:
public function actionResource()
{
$resourcegrporgs = Organization::find()->where(['groupid' => '1'])->all();
return $this->render('resource', [
'resourceGroups' => $resourcegrporgs,
]);
}
然后在你看来,内部的foreach循环应该就是这样:
<?php foreach ($resourceGroup->members as $resourceMember): ?>
<div class="col-lg-3 col-md-3 col-sm-4 col-xs-12 profile">
<div class="img-box">
<img src=<?= $resourceMember->picture ?> class="img-responsive">
<ul class="text-center">
<a href="#"><li><i class="fa fa-facebook"></i></li></a>
<a href="#"><li><i class="fa fa-twitter"></i></li></a>
<a href="#"><li><i class="fa fa-linkedin"></i></li></a>
</ul>
</div>
<h1><?= Html::a(Html::encode($resourceMember->name),['memberdetail', 'id' => $resourceMember->id]) ?></h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<?php endforeach; ?>