Question

My system is working fine for small database and my report is generates from at least 5 table of phpmyadmin after certain limit of data load '500 internal server error' will come.I want enhance exporting a report to csv/excel from phpmyadmin using yii for larger database.

Was it helpful?

Solution

I use this extension to export to CSV. http://www.yiiframework.com/extension/csvexport/ I have created an action that I can attach to any controller that I need to export.

<?php

class Csv extends CAction {

    public $field_list;

    public function run() {
        $controller = $this->getController();

         /* Disable the logging because it should not run on this function */
        foreach (\Yii::app()->log->routes as $route)        {
            if ($route instanceof \CWebLogRoute) {
                $route->enabled = false;
            }
        }
        \Yii::import('core.extensions.ECSVExport.ECSVExport');
        //use the existing filters
        $model_name = $controller->modelName();
        $model = new $model_name('search');
        $dataProvider = $model->search();

        $criteria = $dataProvider->criteria;

        //remove the pagination
        $dataProvider->setPagination(false);

        //changing the criteria to only select what we want
        $criteria->select = implode(',', $this->field_list);
        $dataProvider->setCriteria($criteria);

        //export to CSV
        $csv = new \ECSVExport($dataProvider);
        if(isset($_GET['test'])) {
            echo $csv->toCSV();
        } else {
            \Yii::app()->getRequest()->sendFile($controller->modelName() . '_'.date('Y-m-d').'.csv', $csv->toCSV(), "text/csv", false);
            exit();
        }
    }
}

field_list are the fields that I need to export.

For the controller I add

/**
 * @return array actions to be mapped to this controller
 */
public function actions(){
   return array(
    'csv'=>array(
          'class'=>'core.components.actions.Csv',
          'field_list'=>array('t.id', 't.name', 't.status'),
        ),
    );
}
/**

I use the same search as in the controller because it suits me and because I use http://www.yiiframework.com/extension/remember-filters-gridview/ I actually can export exactly what is on the screen. Change the field list to what you need. Remember to give access to the csvAction function.

OTHER TIPS

You can use yiiexcell extension for that: http://www.yiiframework.com/extension/yiiexcel/ it is simple wrapper for PHPExcel http://phpexcel.codeplex.com/releases/view/107442

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top