简体   繁体   中英

Rendering two models in View yii2

I am trying to render two models in the actionView method, but without result. Checked some answers on previous questions and it seems my method is OK, but still do not work.

This is what a made for method view:

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
        ]);
    }

and my view page:

<?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>

It tells that the author variable is undefined but cannot realize why when i render it in the action. Will appreciate every advice! Thank you!

Try using:

<div class="post-view">

    <div class="content">
        <div class="row">
            <div class="col-md-3">
              <?= $model->content; ?>
             <?= $author->username; ?>
          </div>
        </div>
    </div>

</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM