8

I used the CRUD generator in Yii2 and it generated the following code for my actionIndex controller...

public function actionIndex()
{
    $searchModel = new LeadSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

I am trying to do two things to this default code:

1) Set the page size so that the gridview displaying it only shows 10 rows

2) Modify the $searchModel such that it only returns records where the status column in the table matches certain multiple values (IN Operator)... or better yet, all records that don't match a given value.

For #1, I see many examples to set the 'pagination' while using ActiveDataProvider, but none for search(). This code didn't work for me...

$dataProvider = $searchModel->search(
    Yii::$app->request->queryParams, ['pagination' => [ 'pageSize' => 10 ]]
);

For #2, I know we can filter by declaring the new LeadSearch object as...

$searchModel = new LeadSearch([ 'status' => 'open' ]);

...but something like this doesn't work...

$searchModel = new LeadSearch([ 'status' => ['open', 'pending'] ]);
GAMITG
  • 3,810
  • 7
  • 32
  • 51
Shahid Thaika
  • 2,133
  • 5
  • 23
  • 59
  • Your first question maybe can be answered here [Answer](http://stackoverflow.com/questions/23336269/how-to-create-pager-in-yii2) and I guess you can add this query filter in the `$query` object inside the search_model. Add some `$query->andWhere([ 'status' => ['open', 'pending'] ])` – Thiago Augustus Oliveira Sep 09 '15 at 16:42

4 Answers4

15

You need to add pagination option in ActiveDataProvider in search model and also put where or andWhere at $query condition.

$query = modalName::find()->andWhere([ 'status' => ['open', 'pending'] ]);

$dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [ 'pageSize' => 10 ],
        ]);
GAMITG
  • 3,810
  • 7
  • 32
  • 51
6
    $searchModel = new PersonSearch();
    $dataProvider = $searchModel->search(
    Yii::$app->request->queryParams);
    $dataProvider->pagination = ['pageSize' => 10,];
Alexei
  • 71
  • 1
  • 1
2

Following Yii's getting started tutorial, $dataProvider has a setPagination method.

...
/** Basic generated code from Gii **/
$searchModel = new AlbumSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

/** Paginate & Show 5 items in the table */
$dataProvider->setPagination(['pageSize' => 5]); 
...

The top answer is excessive because it creates a new ActiveDataProvider(...) and re-inserts its own query just to set the pagination. Please be guided.

KevDev
  • 59
  • 4
-1

enter image description here

$dataProvider->pagination = ['pageSize' => 10];
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
  • 1
    Don't post code as images in the answers. It is not necessary. Stack Overflow can display code properly, and it's more useful if it's just text. – giusti Sep 05 '19 at 03:15
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep Sep 05 '19 at 20:31