M
M
Mikhail Khrustalev2017-08-24 22:53:28
Yii
Mikhail Khrustalev, 2017-08-24 22:53:28

Yii2 pagination problem, GridView pagination how?

Good afternoon. Without a bunch of 2 tables, pagination works fine, but it doesn't work with hasMany.
controller:

$users = User::find()->with('posts')->where(['email' => $_SESSION['auth']]);

$countQuery = clone $users;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => 3]);
$models = $users->offset($pages->offset)
        ->limit($pages->limit)
        ->all();


    $this->view->title = "Личный кабинет";
    return $this->render('index', compact('postForm', 'models', 'pages'));

view:
<div id="posts" class="panel-body">

            <?php foreach ($models as $user): ?>
                <?php $posts = $user->posts ?>

                <?php foreach ($posts as $post): ?>
                    <h2><?= $post->title ?></h2>
                    <p><?= $post->text ?></p>
                <?php endforeach; ?>

            <?php endforeach; ?>

            <?php echo LinkPager::widget([
                'pagination' => $pages,
                'registerLinkTags' => true
            ]); ?>

        </div>

As a result, at startup, all posts from the current user are displayed, but alas, there is no Limit (3) - as specified in the controller, and the pagination widget is not displayed in the form. No errors...
I'm trying to apply a GridView, but I don't understand how to display a link to the posts
Controller table:
$dataProvider = new ActiveDataProvider(['query' => User::find()->asArray()->where(['email' => $_SESSION['auth']])->with('posts'),]);

$this->view->title = "Личный кабинет";
return $this->render('index', compact('postForm', 'users', 'dataProvider'));

view:
<div id="posts" class="panel-body">
<?= GridView::widget([
'dataProvider' => $dataProvider,
]); ?>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-08-25
@qonand

As a result, at startup, all posts from the current user are displayed, but alas, there is no Limit (3) - as specified in the controller, and the pagination widget is not displayed in the form. No errors occur...

And it cannot happen, the limit is indicated on the main sample and not on the relation ... i.e. in fact you want to create one pagination for each user?
GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'posts.title',
        'posts.text'
    ]
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question