Join 如何访问yii2中联接表的属性
我的控制器中有两个模型(用户、引用表单)连接在一起。连接工作正常,但我在访问ReferralsForm中的属性时遇到问题。以下是我的控制器中的代码:Join 如何访问yii2中联接表的属性,join,yii2,yii2-basic-app,Join,Yii2,Yii2 Basic App,我的控制器中有两个模型(用户、引用表单)连接在一起。连接工作正常,但我在访问ReferralsForm中的属性时遇到问题。以下是我的控制器中的代码: public function actionReferrals() { $idOfCurrentUser = Yii::$app->user->identity->id; $query = User::find()->joinWith('referrals')->where(['userid' =>
public function actionReferrals()
{
$idOfCurrentUser = Yii::$app->user->identity->id;
$query = User::find()->joinWith('referrals')->where(['userid' => $idOfCurrentUser])->all();
$model = new User();
$ref_hash = $model->getHash();
return $this->render('referrals' ,['query' => $query, 'ref_hash' => $ref_hash]);
}
我的看法是:
(...)
<table class="table table-hover">
<tbody><tr>
<th>#</th>
<th>Referred Username</th>
<th>Status</th>
</tr>
<tr>
<?php foreach ($query as $result){ ?>
<td><?php echo $result->user_id?></td>
<td><?php echo $result->display_name?></td>
<td><?php echo $result->status?></td>
<?php } ?>
</tr>
</tbody>
</table>
(...)
我想从ReferrasForm中访问
状态
,但是它不包括ReferralsForm中的属性,只包括来自用户的属性,即使我已经加入了他们并添加了我读到的类似问题中的all()
。使用relationName->attributeName
<td> <?= $result->referrals->status ?> </td>
如果您有
hasMany()
关系,那么您需要在$result->refererals
中循环,或者使用$result->refererals[0]->status
我需要的是[0]
。非常感谢。
<td> <?= $result->referrals->status ?> </td>