Yii2 在Yii中从控制器传递到视图时,值为null

Yii2 在Yii中从控制器传递到视图时,值为null,yii2,Yii2,为什么我不能将值从控制器传递到视图?该值不为null,但该值似乎未传递到视图页面。为什么? 下面是我的代码: 控制器(SiteController.php): 查看(profileuser.php): 在控制器中检索 型号,all()。如果只需要一个,请使用一个() 您应该使用类似于Gii生成的代码: $model = User::findOne($session['UserID']); if ($model === null) { throw new ForbiddenHttpExce

为什么我不能将值从控制器传递到视图?该值不为null,但该值似乎未传递到视图页面。为什么?

下面是我的代码:

控制器(SiteController.php): 查看(profileuser.php):
在控制器中检索 型号,
all()
。如果只需要一个,请使用一个()


您应该使用类似于Gii生成的代码:

$model = User::findOne($session['UserID']);
if ($model === null) {
    throw new ForbiddenHttpException('You are not authorized to view this page.');
}
它将从数据库中获取
用户
模型,如果请求的用户不存在,则抛出异常

one()
findOne()
不保证它将返回非null,因此您需要自己检查它


\yii\db\Query()
用于原始查询。它不会向您返回模型。在最好的情况下,您将得到一个数组形式的查询结果。

当我使用一个()时,它可以工作,但我将$model->Name更改为$model['Name';在视野中。Idk为什么箭头符号不起作用。因为箭头符号返回数组。。并且没有更新对象答案。。
<?= $model->Name; ?>
public function actionProfileuser()
{
    $session = Yii::$app->session;

    if($session['UserID'] == null){
        Yii::$app->user->logout();
        return $this->goHome();
    }
    else
    {
        //$model = User::find()->where(['ID'=>$session['UserID']])->all();
        //$model = new User;
        $model = (new \yii\db\Query())
            ->from('tbl_user')
            ->where(['ID' => $session['UserID']])
            ->one(); // <---------- use one()
        return $this->render('profileuser', [
            'model' => $model,
        ]);
    }
}
<?= $model['Name']; ?>
foreach( $model AS $key => $value){

    echo $value->name;
}
$model = User::findOne($session['UserID']);
if ($model === null) {
    throw new ForbiddenHttpException('You are not authorized to view this page.');
}