I’ve got a relation defined at my ‘clientes’ model (generated by gii) like this:
'distrito' => array(self::BELONGS_TO, 'Distritos', 'Region'),
I would like to export to excel a field from the related table.
My export code:
public function actionExcel() {
$model=Yii::app()->user->getState('clientesModel');
$_GET['sort']=Yii::app()->user->getState('clientes_sort');
$dataProvider = $model->search();
$dataProvider->pagination= false; // for retrieve all models
$this->toExcel($dataProvider,
array(
'id',
'CompanyName',
'CustomerTaxId',
'Email',
'Website',
'Telephone',
'telefone_2',
'telemovel',
'Fax',
'PostalCode',
'loc_codpostal',
'Country',
'Contact',
'login',
'notas',
'AddressDetail',
'City',
array(
'name' => 'distrito',
'header'=>'Distrito',
'value' => '$data->distrito->nome', // <-- this works fine within cgridview, but not here. (!)
),
),
'Clientes',
array(
'creator' => 'Skeyra',
),
'Excel5'
);
}
I understand that something has to be made at model->search() to retrieve the corresponding data from the related table, but I just can’t figure it out.
my model->search() goes like this:
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('CompanyName',$this->CompanyName,true);
$criteria->compare('CustomerTaxId',$this->CustomerTaxId,true);
$criteria->compare('Telephone',$this->Telephone,true);
$criteria->compare('telefone_2',$this->telefone_2,true);
$criteria->compare('telemovel',$this->telemovel,true);
$criteria->compare('Fax',$this->Fax,true);
$criteria->compare('PostalCode',$this->PostalCode,true);
$criteria->compare('loc_codpostal',$this->loc_codpostal,true);
$criteria->compare('Country',$this->Country,true);
$criteria->compare('Email',$this->Email,true);
$criteria->compare('Website',$this->Website,true);
$criteria->compare('Contact',$this->Contact,true);
$criteria->compare('notas',$this->notas,true);
$criteria->compare('login',$this->login,true);
$criteria->compare('AddressDetail',$this->AddressDetail,true);
$criteria->compare('City',$this->City,true);
$criteria->compare('Region',$this->Region);
$sorter = new CSort;
$sorter->defaultOrder = 'id';
$sorter->attributes = array(
'id'=>'id',
'CompanyName'=>'CompanyName',
'CustomerTaxId'=>'CustomerTaxId',
'Email'=>'Email',
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sorter,
'pagination'=>array('pageSize'=>25),
));
}
}
Can anyone pls help?
Thank you