K
K
kedavr132021-07-09 17:30:04
Yii
kedavr13, 2021-07-09 17:30:04

Why does Yii2 pagination consider related models?

Added work with the associated model to the search method. Everything works, but pagination counts related models too. I output like this(carNumbers.car_number')

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            [
                'class' => 'yii\grid\CheckboxColumn',
                'name' => 'id'
            ],
            'name',
            'phone',
            [
                    'attribute' => 'carNumbers.car_number',
                    'value' => function ($model) {
                    $car_numbers = '';
                    foreach ($model->carNumbers as $car_number){
                        if($car_numbers == ''){
                            $car_numbers = $car_number->car_number;
                        }else{
                            $car_numbers .= ' '.$car_number->car_number;
                        }
                    }

                    return $car_numbers;
                },
            ],

Added to search
$query->joinWith(['carNumbers']);
        $query->andFilterWhere(['LIKE', 'carNumbers.car_number', $this->getAttribute('carNumbers.car_number')]);

It turns out that I should have 10 users on the page, but if one of the users has more than one car number, then it displays fewer users.
As in the screenshot, the selected user has three car numbers, and, accordingly, only 8 users are displayed60e85d69a13f6672248122.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vitaly_74, 2021-07-09
@vitaly_74

you say that you are using carNumbers.car_number, but in the gridview method you have written (most likely) a connection by many, and the connection by many is being processed. everything seems to be logical.

K
kedavr13, 2021-07-13
@kedavr13

If someone gets to this question with a similar problem, I’ll say that replacing joinWith with with solves it, but it breaks the search, I just finalized the search separately, I didn’t find how to solve the search problem within the framework itself, I made an additional request and just throw it in search preselected id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question