Hi, i’m trying to implement an “export GridView” functionality in my project without the use of an extension.
I’m using PHPExcel library to generate excel files, but so far i’ve only been able to do it by re-making the $dataProvider query again with Query Builder (basically, applying the same filters to a new query again). I was looking at the cache features of Yii2 and thought I may be able to save the query data in cache and use it afterwards in a differente controller action to generate to excel, but i know very little about the use of cache for this purpose.
Is it possible to save the query in cache and use it afterwards this way? something like (in searchModel.php):
you are on the right track, you can cache the query and render the excel file in a separate action, there is one problem though your logic for caching and query will not work $query variable will not be available directly in your callback you have to pass it down explicitly like so, also lose that key param yii will take care of it.
I’ll implement a “query” method in my model for sure, it will keep the code much cleaner. I still got a couple of questions about using cache, mainly:
1.- How can i access this "result" from my controller when i intend to export the data to excel? i was reading the docs and it seems (to me at least) that you can only access data from the cache with a defined key, or is there another way to do it?
2.- I should flush or clear the cache after every query or a certain amount of time, right? to prevent it from being flooded with query results.
My idea is to perform the filters on the gridview (applied in action “person/index”), save that query result in cache, and use it later when the user wants to export these results (action “person/exportResults”). So i’ll need to access the cached results from “person/exportResults”, i think at least.