E
E
EVOSandru62017-08-19 21:11:16
.NET
EVOSandru6, 2017-08-19 21:11:16

Why does dataProvider break in YII2?

Good afternoon.
I don't understand the reason. Googled a lot, honestly.
There is this code:

$className = $this->className;
        $query = $className::find();
        $query->andWhere([
            $this->attribute=>$this->model->id
        ]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 2,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                    'name' => SORT_ASC,
                ]
            ],
        ]);

Next, I make sure that the record provider has: Next, I throw this $dataProvider into the ListView:
echo $dataProvider->getTotalCount(). '<br/>'; // 3
echo ListView::widget([
                'dataProvider' => $dataProvider,
                'itemView' => '_list_item',
                'options'=> [
                    'id'=>'comments-list',
                ],
                'itemOptions' => [
                    'class' => 'comments-item'
                ],
            ]);

And I catch this error:

PHP Warning - yii\base\ErrorException
Invalid argument supplied for foreach()

What could be wrong?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
nApoBo3, 2019-04-19
@Collin

Free Spire.PDF for .NET
You probably have a paid version. Free prints fine.
Free is limited to 10 page pdf.
Native pdf printing, as far as I understand, depends on the printer. We have kyocera and hp eat pdf without any problems, though we send it with cups, and not from the user's machine (via a web service).

A
Alexander, 2019-04-19
@NeiroNx

I somehow did it through Sumatra
When I had to print from a script.

E
EVOSandru6, 2017-08-20
@EVOSandru6

Thanks for the responses, I found out that the error is in the part:

'defaultOrder' => [
                    'id' => SORT_DESC,
                    // 'name' => SORT_ASC,
                ]

The name field is not in the table. It is strange that this was not mentioned in the debug. I will know now.

D
Dmitry, 2017-08-19
@slo_nik

Goodnight.
What if you change your code like this?

$query = self::find(); // или напрямую подставьте модель
        $query->where([
            $this->attribute=>$this->model->id
        ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question