[Yii2] how to check if dataProvider is empty

Hi all, i want to know if a $dataProvider is empty or not.

Something similar to


       if (! empty($dataProvider)) {

           //design my GridView

        }

obviously this code doesn’t work because $dataProvider is never empty, also when there are no row retrieved.

So, what is the best method to check this?

Thank’s

I think it depends on what exactly you need to check for, when you say "empty". If the object itself is instantiated as a DataProviderInterface object, then indeed, it wont be empty.

Are you trying to check how many records of a given model or models were returned in a query made by the dataProvider? If so, there are a number of ways to check, but its likely that $dataProvider->totalCount would suffice. If you have 0, then your query selects no records.

1 Like

Thank you Stratigos is exactly what i need:




if ($dataProvider->totalCount > 0) {

1 Like