Answer the question
In order to leave comments, you need to log in
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'));
<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>
$dataProvider = new ActiveDataProvider(['query' => User::find()->asArray()->where(['email' => $_SESSION['auth']])->with('posts'),]);
$this->view->title = "Личный кабинет";
return $this->render('index', compact('postForm', 'users', 'dataProvider'));
<div id="posts" class="panel-body">
<?= GridView::widget([
'dataProvider' => $dataProvider,
]); ?>
</div>
Answer the question
In order to leave comments, you need to log in
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...
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'posts.title',
'posts.text'
]
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question