Export GridView as excel (.xlsx) without extensions

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):




$query = Person::find();


$dataProvider = new ActiveDataProvider([

'query' => $query,

]);


$query->andFilterWhere(['name' => 'Erick']);


$key = 'some value';


...

$result = Person::getDb()->cache($key, function ($db) {

    return $query;

});

...



Or am i better of exporting the gridview in some other way?

Any help is highly appreciated, thanks.

hi,

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.


$result = Person::getDb()->cache(function ($db) use($query) {

    return $query;

});



if you would like to improve this code, add a method in your model that queries your database and returns the result that will clean up your controllers, here is something to get you going




<?php 

// your model class

class Person extends ActiveRecord {


	//...

	

	public static function findByName($name)

	{

		$person = static::getDb()->cache(function ($db) use($name) {

		    $query = static::find();

		    $query->andFilterWhere(['name' => $name]);

			$dataProvider = new ActiveDataProvider([

				'query' => $query,

			]);

			return $dataProvider;

		});


		return $person;

	}


	//...

}

?>


<?php


class PersonController extends Controller {


    /**

     * Displays person.

     *

     * @return string

     */

    public function actionIndex()

    {

        //...

        $person = Person::findByName($name);

        //...

    }


}






Thanks for the quick response alrazi.

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.