title: Pagination with array in yii2
body: <p>I want to use pagination in my view, but I can't figure out how to do it in conjunction with the <code>find()</code> method.</p>
  
  <p>Getting the numbers of pages works correctly, but it always displays all values from the database. I want to see 15 comments per page.</p>
  
  <p>Here is my ViewAction controller:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code> public function actionView($id) {
       $query = UrComment::find()-&gt;where(['IsDeleted' =&gt; 0]);
  
       $pagination = new Pagination(['totalCount' =&gt; $query-&gt;count(), 'pageSize'=&gt;12]);
       return $this-&gt;render('view', [
              'model' =&gt; $this-&gt;findOneModel($id),
              'comment' =&gt; UrComment::findComment($id),
              'pagination'=&gt; $pagination
       ]);
  }
  </code></pre>
  
  <p>And this is how I get the comments:</p>
  
  <pre class="lang-php prettyprint-override"><code>public static function findComment($id)
  {
      if (($model = UrUser::findOne($id)) !== null) {
          $Id=$model-&gt;Id;
          $comment = UrComment::find()-&gt;where(['Rel_User' =&gt; $Id])-&gt;all();
          return $comment;
      } else {
          throw new NotFoundHttpException('The requested page does not exist.');
      }
  }
  </code></pre>
  
  <p>I tried to use this:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function actionView($id) {
      $query = UrComment::find()-&gt;where(['IsDeleted' =&gt; 0]);
      $pagination = new Pagination(['totalCount' =&gt; $query-&gt;count(), 'pageSize'=&gt;12]);
      $comment= UrComment::findComment($id);
      $comment-&gt;offset($pagination-&gt;offset)
          -&gt;limit($pagination-&gt;limit)
          -&gt;all();
  
      return $this-&gt;render('view', [
                  'model' =&gt; $this-&gt;findOneModel($id),
                  'comment' =&gt;$comment,
                  'pagination'=&gt; $pagination
      ]);
  }
  </code></pre>
  
  <p>But I get this error:</p>
  
  <blockquote>
    <p>Call to a member function offset() on array</p>
  </blockquote>
  
  <p>Then I display it in the view:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\widgets\DetailView;
  use yii\helpers\Url;
  /* @var $this yii\web\View */
  /* @var $model backend\modules\users\models\UrUser */
  
  $this-&gt;title = $model-&gt;Name;
  $this-&gt;params['breadcrumbs'][] = ['label' =&gt; Yii::t('app', 'Użytkownicy'), 'url' =&gt; ['index']];
  $this-&gt;params['breadcrumbs'][] = $this-&gt;title;
  ?&gt;
  &lt;div class="ur-user-view"&gt;
  
      &lt;h1&gt;&lt;?= Html::encode($this-&gt;title) ?&gt;&lt;/h1&gt;
  
      &lt;p&gt;
          &lt;?= Html::a(Yii::t('app', 'Aktualizuj'), ['update', 'id' =&gt; $model-&gt;Id], ['class' =&gt; 'btn btn-primary']) ?&gt;
          &lt;?=
          Html::a(Yii::t('app', 'Usuń'), ['delete', 'id' =&gt; $model-&gt;Id], [
              'class' =&gt; 'btn btn-danger',
              'data' =&gt; [
                  'confirm' =&gt; Yii::t('app', 'Jesteś pewien, że chesz usunąć tego użytkownika?'),
                  'method' =&gt; 'post',
              ],
          ])
          ?&gt;
      &lt;/p&gt;
  
      &lt;?=
      DetailView::widget([
          'model' =&gt; $model,
          'attributes' =&gt; [
              'Id',
              'Name',
              'Surname',
              'BirthDate',
              'Login',
              'Email:email',
              ['attribute' =&gt; 'Rel_Sex', 'value' =&gt; (isset($model-&gt;relSex-&gt;Name)? $model-&gt;relSex-&gt;Name : "Nie wybrano")],
              ['attribute' =&gt; 'Rel_Language', 'value' =&gt; (isset($model-&gt;relLanguage-&gt;Name)) ? $model-&gt;relLanguage-&gt;Name : "Nie wybrano"],
              ['attribute' =&gt; 'Rel_Country', 'value' =&gt; (isset($model-&gt;relCountry-&gt;Name)) ? $model-&gt;relCountry-&gt;Name : "Nie wybrano"],
              ['attribute' =&gt; 'Rel_Category', 'value' =&gt; (isset($model-&gt;relUserCategory-&gt;Name)) ? $model-&gt;relUserCategory-&gt;Name : "Nie wybrano"],
          ],
      ])
      ?&gt;
  
  Komentarze użytkownika: &lt;?= $model-&gt;Name.' '.$model-&gt;Surname;?&gt;&lt;br&gt;&lt;br&gt;
  
  
      &lt;?php foreach ($comment as $comm): ?&gt;
  
       &lt;?= Html::a(Yii::t('app', 'Edytuj'), ['update-user-comment', 'id' =&gt; $comm-&gt;Id], ['class' =&gt; 'btn btn-primary']) ?&gt;
          &lt;?=
          Html::a(Yii::t('app', 'Usuń'), ['delete-comment', 'id' =&gt; $comm-&gt;Id], [
              'class' =&gt; 'btn btn-danger',
              'data' =&gt; [
                  'confirm' =&gt; Yii::t('app', 'Jesteś pewien, że chesz usunąć tego użytkownika?'),
                  'method' =&gt; 'post',
              ],
          ])
          ?&gt;
              &lt;p&gt;&lt;?= $comm-&gt;Text ?&gt;&lt;/p&gt;
  
      &lt;hr&gt;
      &lt;?php endforeach; ?&gt;
  &lt;?php
     echo \yii\widgets\LinkPager::widget([
      'pagination' =&gt; $pagination,
  ]);
  ?&gt;
  
  &lt;/div&gt;
  &lt;/div&gt;    
  &lt;/div&gt;
  </code></pre>
  
  <hr>
  
  <p>How can I limit the <code>UrComment::findComment($id)</code> using the pagination?</p>
  
  <p>EDIT:</p>
  
  <p>I think I understand you, and I think I've done everything you told me in your answer, but now I have another problem. I need to display under view comments only that which is id view only this one person not all comments.</p>
  
  <p>Here is what I have now:</p>
  
  <p>ActionView:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function actionView($id) {
      $searchModel = new CommentSearch();
      $dataProvider = $searchModel-&gt;search(Yii::$app-&gt;request-&gt;queryParams);
      $dataProvider-&gt;query-&gt;andWhere(['IsDeleted' =&gt; 0]);
      $dataProvider-&gt;pagination-&gt;pageSize=15;
  
      return $this-&gt;render('view', [
                  'model' =&gt; $this-&gt;findOneModel($id),
                  'comment' =&gt; UrComment::findComment($id),
                  'searchModel' =&gt; $searchModel,
                  'dataProvider' =&gt; $dataProvider,
      ]);
  }
  </code></pre>
  
  <p>CommentSearch:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace backend\modules\users\models;
  
  use Yii;
  use yii\base\Model;
  use yii\data\ActiveDataProvider;
  use common\models\UrComment;
  
  class CommentSearch extends UrComment
  {        
      public function rules() {
          return [
              [['Id'], 'integer'],
              [['Text'], 'safe'],           
          ];
      }
  
      public function scenarios() {
          // bypass scenarios() implementation in the parent class
          return Model::scenarios();
      }
  
      public function search($params) {
          $query = UrComment::find();
  
          $dataProvider = new ActiveDataProvider([
              'query' =&gt; $query,
          ]);
  
          $this-&gt;load($params);
  
          if (!$this-&gt;validate()) {
              // uncomment the following line if you do not want to return any records when validation fails
              // $query-&gt;where('0=1');
              return $dataProvider;
          }
  
          $query-&gt;andFilterWhere([
              'Id' =&gt; $this-&gt;Id,
              'Text' =&gt; $this-&gt;Text,                
          ]);
  
          $query-&gt;andFilterWhere(['like', 'Text', $this-&gt;Text]);
  
          return $dataProvider;
      }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\widgets\DetailView;
  use yii\helpers\Url;
  use yii\widgets\ListView;
  /* @var $this yii\web\View */
  /* @var $model backend\modules\users\models\UrUser */
  
  $this-&gt;title = $model-&gt;Name;
  $this-&gt;params['breadcrumbs'][] = ['label' =&gt; Yii::t('app', 'Użytkownicy'), 'url' =&gt; ['index']];
  $this-&gt;params['breadcrumbs'][] = $this-&gt;title;
  ?&gt;
  &lt;div class="ur-user-view"&gt;
  
      &lt;h1&gt;&lt;?= Html::encode($this-&gt;title) ?&gt;&lt;/h1&gt;
  
      &lt;p&gt;
          &lt;?= Html::a(Yii::t('app', 'Aktualizuj'), ['update', 'id' =&gt; $model-&gt;Id], ['class' =&gt; 'btn btn-primary']) ?&gt;
          &lt;?=
          Html::a(Yii::t('app', 'Usuń'), ['delete', 'id' =&gt; $model-&gt;Id], [
              'class' =&gt; 'btn btn-danger',
              'data' =&gt; [
                  'confirm' =&gt; Yii::t('app', 'Jesteś pewien, że chesz usunąć tego użytkownika?'),
                  'method' =&gt; 'post',
              ],
          ])
          ?&gt;
      &lt;/p&gt;
  
      &lt;?=
      DetailView::widget([
          'model' =&gt; $model,
          'attributes' =&gt; [
              'Id',
              'Name',
              'Surname',
              'BirthDate',
              'Login',
              'Email:email',
              ['attribute' =&gt; 'Rel_Sex', 'value' =&gt; (isset($model-&gt;relSex-&gt;Name)? $model-&gt;relSex-&gt;Name : "Nie wybrano")],
              ['attribute' =&gt; 'Rel_Language', 'value' =&gt; (isset($model-&gt;relLanguage-&gt;Name)) ? $model-&gt;relLanguage-&gt;Name : "Nie wybrano"],
              ['attribute' =&gt; 'Rel_Country', 'value' =&gt; (isset($model-&gt;relCountry-&gt;Name)) ? $model-&gt;relCountry-&gt;Name : "Nie wybrano"],
              ['attribute' =&gt; 'Rel_Category', 'value' =&gt; (isset($model-&gt;relUserCategory-&gt;Name)) ? $model-&gt;relUserCategory-&gt;Name : "Nie wybrano"],
          ],
      ])
      ?&gt;
  Komentarze użytkownika: &lt;?= $model-&gt;Name.' '.$model-&gt;Surname;?&gt;&lt;br&gt;&lt;br&gt;
  &lt;?php    
    echo ListView::widget([
     'dataProvider' =&gt; $dataProvider,
     'itemView' =&gt; 'comments',
     'viewParams' =&gt; ['comment' =&gt; $comment, 'model'=&gt;$model],
  ]);
  ?&gt;
  &lt;/div&gt;
  &lt;/div&gt; 
  &lt;/div&gt;
  </code></pre>
  
  <p>And my item comments:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\widgets\DetailView;
  use yii\helpers\Url;
  /* @var $this yii\web\View */
  /* @var $model backend\modules\users\models\UrUser */
  
  
  ?&gt; 
       &lt;?= Html::a(Yii::t('app', 'Edytuj'), ['update-user-comment', 'id' =&gt; $comment-&gt;Id], ['class' =&gt; 'btn btn-primary']) ?&gt;
          &lt;?=
          Html::a(Yii::t('app', 'Usuń'), ['delete-comment', 'id' =&gt; $comment-&gt;Id], [
              'class' =&gt; 'btn btn-danger',
              'data' =&gt; [
                  'confirm' =&gt; Yii::t('app', 'Jesteś pewien, że chesz usunąć tego użytkownika?'),
                  'method' =&gt; 'post',
              ],
          ])
          ?&gt;
              &lt;p&gt;&lt;?= $comment-&gt;Text ?&gt;&lt;/p&gt;
  
      &lt;hr&gt;
  </code></pre>
  
  <p>Using that code, now I have error that in this item in button edit:  </p>
  
  <blockquote>
    
    $comment->Id], ['class' => 'btn btn-primary']) ?>
    
    <p>Trying to get property of non-object</p>
  </blockquote>
  
  <p>And I can't use foreach there because I have duplicate comments. Should change something in comment Search or in my function <code>findComment(id)</code> inquiry?</p>
  
  <p>There is my actual item 'comments' where I want to display text of comments and buttons to edit and delete comment. But this does not work for me. I have:</p>
  
  <blockquote>
    <p>Use of undefined constant Id - assumed 'Id'</p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p>Use of undefined constant Text - assumed 'Text';</p>
  </blockquote>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\widgets\DetailView;
  use yii\helpers\Url;
  /* @var $this yii\web\View */
  /* @var $model backend\modules\users\models\UrUser */
  
  
  ?&gt; 
       &lt;?= Html::a(Yii::t('app', 'Edytuj'), ['update-user-comment', 'id' =&gt; Id], ['class' =&gt; 'btn btn-primary']) ?&gt;
          &lt;?=
          Html::a(Yii::t('app', 'Usuń'), ['delete-comment', 'id' =&gt; Id], [
              'class' =&gt; 'btn btn-danger',
              'data' =&gt; [
                  'confirm' =&gt; Yii::t('app', 'Jesteś pewien, że chesz usunąć tego użytkownika?'),
                  'method' =&gt; 'post',
              ],
          ])
          ?&gt;
              &lt;p&gt;&lt;?= Text ?&gt;&lt;/p&gt;
      &lt;hr&gt;
  </code></pre>