I have what i know is a simple problem, but it has got me beat.
I have two tables, strains and toxin, where one bacterial strain has many different toxins.
My problem is that I can populate a gridview table with data from strains, but not the related data from a field called ‘type’ in table toxins.
The relationship that table strains has with table toxin is:
public function relations()//(note that rid_strain is the FK in table toxin)
{
return array(
'toxins' => array(self::HAS_MANY, 'Toxin', 'rid_strain'),
);
}
The controller is:
public function actionIndex()
{
$model=new Strains('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Strains']))
$model->attributes=$_GET['Strains'];
$this->render('index',array(
'model'=>$model,
));
}
In the Model I also have:
public function search() //note that i have not put any of the filtering stuff in yet since i can’t populate the field ‘type’ from associated table ‘toxin’
{
$criteria=new CDbCriteria;
$criteria->compare('rid',$this->rid);
$criteria->compare('strain',$this->strain,true);
$criteria->compare('gene',$this->gene,true);
$criteria->compare('summary',$this->summary,true);
$criteria->compare('date',$this->date,true);
$criteria->compare('environment',$this->environment,true);
$criteria->compare('serotype',$this->serotype,true);
$criteria->compare('country',$this->country,true);
$criteria->compare('date_isolated',$this->date_isolated,true);
return new CActiveDataProvider($this,array(
'criteria'=>$criteria,
));
}
and the view is:
<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'strains-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
// removing one of these attributes will remove the column from the search form
'strain',
'gene',
'summary',
'date',
'environment',
'toxins.type', //I added this on top of gii-generated code.
'rid',
array(
'class'=>'CButtonColumn',
'template'=>'{update}{view}{delete}',
'buttons'=>array(
'update'=>array(
'visible'=>'false',
),
'view'=>array(
'visible'=>'true',
),
'delete'=>array(
'visible'=>'false',
),
),
),
),
)); ?>