Hi,
Here's my solution:
in the view I use
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'article-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'nom',
'prix',
'definition',
/*'mark_owner.nom_marque',*/
array( // display 'author.username' using an expression
'name'=>'marque_id',
'value'=>'$data->mark_owner->nom_marque',
),
'image',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
marque_id have as value: $data->mark_owner->nom_marque
And in my model I use something like that and its works:
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'mark_owner'=>array(self::BELONGS_TO,'Marque','marque_id'),
'line_items'=>array(self::HAS_MANY,'LineItem','article_id')
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'nom' => 'Nom',
'prix' => 'Prix',
'definition' => 'Definition',
'marque_id' => 'Marque',
'image' => 'Image',
);
}
public function behaviors(){
return array( 'EAdvancedArBehavior' => array(
'class' => 'application.extensions.EAdvancedArBehavior'));
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
//var_dump($this);
//exit(0);
//echo 'gggg'.$this->prix;
//exit(0);
/*->find (
array(':nom_marque'=>$this->marque_id));*/
$criteria=new CDbCriteria;
$criteria->with= 'mark_owner';
$criteria->compare('id',$this->id);
$criteria->compare('nom',$this->nom,true);
$criteria->compare('prix',$this->prix);
$criteria->compare('definition',$this->definition,true);
if(!empty($_GET['Article']))
$criteria->compare('mark_owner.nom_marque',Marque::model()->find(array('condition'=>"nom_marque like '%".$_GET['Article']['marque_id']."%'"))->nom_marque, true );
$criteria->compare('image',$this->image,true);
$criteria->compare('image_zoom',$this->image_zoom,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
I think this code will help all.
Roodly
egidijusb, on 29 April 2010 - 06:30 AM, said:
Hi,
i have two table, one with second relation
public function relations()
{
return array(
'kat'=>array(self::BELONGS_TO, 'Kategorijos', 'kategorijos_id'),
);
}
In CGridView write:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'paslauga-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'kodas',
'pavadinimas',
'intervalas',
array(
'name'=>'kat.pavadinimas',
'value'=>'$data->kat->pavadinimas',
),
)
)); ?>
but not search or sort in columns 'kat.pavadinimas' not works. Sort working, when i replace 'name'=>'kat.pavadinimas', to 'name' => 'kat_id'. What is wrong :/ ?