【问题标题】:Rendering two models in View yii2在 View yii2 中渲染两个模型
【发布时间】:2017-03-21 08:06:06
【问题描述】:

我试图在 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>

它告诉作者变量是未定义的,但是当我在动作中渲染它时无法理解为什么。将感谢每一个建议!谢谢!

【问题讨论】:

  • 您想针对哪个模型渲染另一个文件?
  • 您确定 $currUser 包含有效模型吗? .. 如果查询没有返回您拥有的有效模型和未定义的值
  • 是的,当我直接在 view.php 中键入代码时,它会返回一个有效的对象作者。用 var_dump 检查它,它似乎是正确的:)

标签: php yii2


【解决方案1】:

尝试使用:

<div class="post-view">

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

</div>

【讨论】:

  • 好的,它是这样工作的!十分感谢!但为什么另一种方式是错误的?再次感谢你:)
  • = variable_name ?> 等价于 。它只能取 1 个变量而不是 2 个。
  • 哦是的..当我以这种方式看到它时=,我总是忘记事实上这是回声。再次感谢您!
猜你喜欢
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多