How to avoid querying the database on initial page load

Hi!

What’s the best way, if any, to prevent data being initially loaded for a view and have it loaded only if search form is being submitted?

I’m using GridView, search model and ActiveDataProvider.

I want to avoid making a db query on load as the table I’m accessing is huge. Even if I restrict the rows that need to be pulled, it’s very likely that the user will change the restrictions anyway (via the search form) to match their needs and trigger a subsequent call to db. So the ideal scenario would be not to perform the initial query at all.

So what I would like to see when my page loads is the search form only and then when the form gets submitted I would like the GridView to display the data that matches the search criteria.

Thanks for any suggestions.

I would tweak my "search" method to return a quick and empty result when the search parameters are in the initial state without any input from the user.

Something like this:




if ($this->key1 == '' && $this->key2 == '' && $this->key3 == '') {

    $query->where(['id' => 0]);

}

...



Thank you very much for your reply! That’s what I’m going with then :)

Small improvement that I’ve made though is using $model->getDirtyAttributes() method to check if there were any changes to the form.