Related Data At Cactivedataprovider

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

Despite those excelent zero answers… This is the code… (now it works…) for those who might be interested:

in model:




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


    $criteria->with = array( 'relDistrito' );

    $criteria->compare( 'relDistrito.nome', $this->relDistrito, true );

    

    if(!empty($this->from) && empty($this->to)):

      $criteria->condition = "dataentrada >= '$this->from'";  

    elseif(!empty($this->to) && empty($this->from)):

      $criteria->condition = "dataentrada <= '$this->to'";

    elseif(!empty($this->to) && !empty($this->from)):

      $criteria->condition = "dataentrada  >= '$this->from' and dataentrada <= '$this->to'";

    endif;

    

    $sorter = new CSort;        

    $sorter->defaultOrder = 't.id';        

    $sorter->attributes = array(

        'id'=>'t.id',

        'CompanyName'=>'CompanyName',

        'CustomerTaxId'=>'CustomerTaxId',

        'Email'=>'Email',

        );


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

      'sort'=>$sorter,

			'pagination'=>array('pageSize'=>25), 

		));

	}

}




at controller:







  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',

            'relDistrito.nome', <--- the important one

        ),

        'Clientes',

        array(

            'creator' => 'Skeyra',

        ),

        'Excel5'

    );






enjoy and be happy !!

Thank you for this.

I’ll try your code.