[英]Rendering two models in View yii2
我正在嘗試在actionView方法中渲染兩個模型,但是沒有結果。 檢查了先前問題的一些答案,看來我的方法還可以,但仍然行不通。
這是為方法視圖制作的:
public function actionView($id)
{
$postModel = Post::find()->where(['post_id' => $id])->one();
$currUser = BlogUser::find()->where(['id' => $postModel->user_id])->one();
return $this->render('view', [
'model' => $this->findModel($id),
'author' => $currUser
]);
}
和我的查看頁面:
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model app\models\Post */
//$this->title = $model->title;
//$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];
//$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-view">
<div class="content">
<div class="row">
<div class="col-md-3">
<?= $model->content;
$author->username;
?>
</div>
</div>
</div>
</div>
它告訴我們author變量是不確定的,但是當我在動作中渲染它時卻無法理解為什么。 將不勝感激每一個建議! 謝謝!
嘗試使用:
<div class="post-view">
<div class="content">
<div class="row">
<div class="col-md-3">
<?= $model->content; ?>
<?= $author->username; ?>
</div>
</div>
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.