yiiphpexcelyii-extensionsyii-events

Dataprovider to EExcelView


lets say i have a table named

cars{
'id','name','brand_id', 
}

and another table

brand{
'id','brand_name',
}

I have a situation that i want to generate an Excel report with the following attributes. 'name','brand_name' i.e. SELECT cars.name, brand.brand_name FROM cars INNER JOIN on brand WHERE cars.brand_id = brand.id

So i created a dataprovider like this:

$sql = "SELECT cars.name, brand.brand_name FROM cars INNER JOIN brand on cars.brand_id = brand.id";
$result = Yii::app()->db->createCommand($sql)->queryAll();
$this->render('doc', array('dataprovider' => $result));

Now i want to generate Excel file with result as a dataProvider so i write the following code:

// lets say i am doing this in view page named doc.php

$factory = new CWidgetFactory(); 
Yii::import('ext.eexcelview.EExcelView',true);  
        $widget = $factory->createWidget($this,'EExcelView', array(
            'dataProvider'=>$dataprovider->search(),
            'grid_mode'=>'export',
            'title'=>'Title',
            'creator'=>'TNC',
            'autoWidth'=>false,
            'filename'=>'Report.xlsx',
            'stream'=>false,
            'disablePaging'=>false,
            'exportType'=>'Excel2007',
            'columns'=>array(
                'name',
                'brand_name',),
            'showTableOnEmpty' => false,
        ));

        $widget->init();
        $widget->run();

I have included all the extensions that i have to.. This code is working when i fed the dataProvider field with a single table entry . But the situation arises when i include multiple tables.


Solution

  • These lines don't actually make a dataprovider:

    $result = Yii::app()->db->createCommand($sql)->queryAll();
     $this->render('doc', array('dataprovider' => $result));
    

    You'll want to do something like the following:

    $dataprovider = new CSqlDataProvider($sql, array(
        'pagination'=>false,
    );
    $this->render('doc', array('dataprovider' => $dataprover);
    

    More info here: http://www.yiiframework.com/doc/api/1.1/CSqlDataProvider