没有从数据库接收所有数据,yii2

时间:2015-11-01 16:46:45

标签: php mysql yii2

用于呈现索引页的控制器操作

public function actionIndex()
    {
        $posts = Posts::find()->all();
        $user = User::find()->all;
        return $this->render('index', [
            'posts' => $posts,
            'user' => $user
        ]);
    }

查看:

<? foreach ($posts as $post){?>
    <div class="well">
        <h5><?=$user[$post->user_id-1]->login ?></h5>
        <?=$post->text ?>
    </div>
    <?} ?>

显示帖子文本但用户不登录。为什么? var_dump($ users)什么都没显示。我查了一下这张桌子。用户表中有一些记录。

1 个答案:

答案 0 :(得分:0)

public function actionIndex()
{
    $posts = Posts::find()->all();
    $user = User::find()->all();
    return $this->render('index', [
        'posts' => $posts,
        'user' => $user
    ]);
}

使用User::find()->all(); User::find()->all;