Question

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.

Était-ce utile?

La 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

Autres conseils

This works for 2 tables, dont know works for more than 2 or not.

$dataProvider = new CArrayDataProvider($dataprovider, array('id' => 'brand', 'sort' => array('attributes' => array('brand_name', ), ), 'pagination' => false));
$this -> render('doc', array('dataprovider' => $dataProvider,));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top