我想在actionView方法中渲染两个模型,但没有结果。检查了以前的问题的一些答案,似乎我的方法是好的,但仍然无法正常工作。在View中渲染两个模型yii2
这是何等的方法观提出:
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>
它告诉笔者变量是不确定的,但不能明白为什么当我在使它行动。将欣赏每一个建议!谢谢!
要渲染针对哪个型号另一个文件? –
你确定$ currUser包含一个有效的模型吗? ..如果查询没有返回一个有效的模型你有和未定义的值 – scaisEdge
是的,它返回一个有效的对象作者时,我直接在view.php中键入代码。检查它与var_dump,它似乎是正确的:) –