Made some modifications …figured my problem was the many_many relationships. Decided to use a function in my model to get my targets, and used it in the view.
Two positives;
-
target info shows up (w/o partial view I was using earlier)
-
search box shows up for target_id
The negative:
The search no longer works … I think the error is in the model file under CDbCriteria …but it can also be in my CGridView.
If someone could help me, this would be appreciated. I think that if I could somehow make the "value=>" that I am showing in the CGridView available to the model, I can make it searchable, and this could maybe work.
(I’m new, so what I am doing may not really make sense).
//Drug.php
public function targetsToString()
{
$targets = $this->targets;
if ($targets)
{
$string = '';
foreach($targets as $target) {
$string .= $target->target_id . ', ';
}
return substr($string,0,strlen($string)-1);
}
return null;
}
....
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('drug_name, drug_indication, drug_synonym, drug_brand', 'required'),
array('drug_name', 'length', 'max' => 100),
array('drug_id', 'length', 'max' => 60),
array('drug_indication', 'length', 'max' => 1700),
array('drug_synonym', 'length', 'max' => 120),
array('drug_brand', 'length', 'max' => 40),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('drug_name, target, drug_id, drug_indication, drug_synonym, drug_brand', 'safe', 'on' => 'search'),
);
}
....
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('t.drug_name', $this->drug_name, true);
$criteria->with = 'targets';
$criteria->together=true;
$criteria->compare('t.drug_id', $this->drug_id, true);
$criteria->compare('targets.target_id', $this->drug_id,true);
$criteria->compare('t.drug_indication', $this->drug_indication, true);
$criteria->compare('t.drug_synonym', $this->drug_synonym, true);
$criteria->compare('t.drug_brand', $this->drug_brand, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
//admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'drug-targets-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
//'nullDisplay' => 'N/A',
'columns'=>array(
'drug_id',
'drug_name',
'drug_synonym',
'drug_brand',
array(
'header' => 'Target Name',
'type' => 'raw',
'value'=>'$this->grid->getOwner()->renderPartial("_drug_target_name", array("data"=>$data), true);',
),
array (
'header' => 'Target ID',
'name' => 'drug_id',
'value' => '$data->targetsToString()',
),
)
)); ?>