Hey everyone,
I have two tables, Samples and Species. Samples has a Foreign Key, "SpeciesID", referring to the Species table having SpeciesID as primary key
I managed to use the relations() function in the SamplesController so that the CGridView in the Admin of the Samples shows the SciName of the Species instead of the SpeciesID. I think relations() is OK.
I am struggling with making it searchable. This kind of question has been asked many times, I know, but somehow I can’t grasp the general idea.
It is probably the search() that I am not doing properly.
My code:
//In Samples
public function rules()
{
return array
(
.....
array('SampleID, SciName, SensorID, ModelNumber, DateTime, Latitude, Longitude, Altitude, SampleType, SampleCondition, Comment, PhotoFilename, SpectrumFile', 'safe', 'on'=>'search'),
)
}
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('GetSciName'=>array(self::BELONGS_TO, 'Species', 'SpeciesID'),
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('SampleID',$this->SampleID);
$criteria->with = 'GetSciName';
$criteria->compare('GetSciName.SciName', $this->SciName, true);//somehow doesnt work?
//The CGridView seems OK, it shows the Species name "SciName" not the SpeciesID... just not searchable (yet)
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'samples-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'emptyText'=>'Not available',
'columns'=>array(
'SampleID',
//'SpeciesID',
array( 'name'=>'SpeciesID',
'value'=>'$data->GetSciName->SciName'),
'DateTime',
'Latitude',
'Longitude',
'Altitude',
'SampleType',
'SampleCondition',
'Comment',
'PhotoFilename',
'SpectrumFile',
array(
'class'=>'CButtonColumn',
),
),
)); ?>