i have 2 models assignment and student related via belongs to relation, i tried to display the full name of the student in the cgridview and make a filter based on it.
the full name is displayed successfully but the filtering is not. why? can anyone tell me the error.
there is my code
in assignment model:
class Assignment extends CActiveRecord
{
public $fullname;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Assignment the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'assignment';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('studentID, status', 'length', 'max'=>15),
array('id, fullname', 'safe', 'on'=>'search'),
);
}
/**
* @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(
'student' => array(self::BELONGS_TO, 'Student', 'studentID'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Assign ID',
'studentID' => 'Student',
);
}
/**
* 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.
$criteria=new CDbCriteria;
$criteria->with = array( 'student' );
$criteria->compare( 'student.fullname', $this->fullname );
$criteria->compare('id',$this->id);
$criteria->compare('studentID',$this->studentID);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
in student model
public function getFullName()
{
//$model = new Student();
return $this->E_Child_Name . " " . $this->E_Father_Name . " " . $this->E_Family_Name;
}
in assignment/admin.php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'assignment-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
array(
'name'=>'studentID',
'type'=>'raw',
'value'=>'CHtml::encode(Student::model()->findByPk($data->studentID)->fullname)',
),