Yii Framework Forum: Related Data At Cactivedataprovider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Related Data At Cactivedataprovider Rate Topic: -----

#1 User is offline   jorge sequeira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 05-February 13

Posted 06 February 2013 - 06:04 PM

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
0

#2 User is offline   jorge sequeira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 05-February 13

Posted 12 February 2013 - 06:20 PM

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 !!
0

#3 User is offline   FlyngThunderGod 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-January 13

Posted 12 February 2013 - 06:44 PM

Thank you for this.

I'll try your code.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users