[font="Courier New"]I have this problem from http://www.yiiframework.com/wiki/281/searching-and-sorting-by-rel
i have two tables
|teacher_status|…|user_account|
-------------- … ------------
|tid…|->|user_id…|
|user_id…|…|user_fname…|
|login_date…|…|user_lname…|
here’s my model
class TeacherStatus extends CActiveRecord
{
public $search_name;
public function tableName()
{
return 'teacher_status';
}
public function rules()
{
return array(
array('user_id, login_date', 'required'),
array('user_id', 'numerical', 'integerOnly'=>true),
array('login_date', 'length', 'max'=>255),
array('id, tid, login_date', '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(
'teachers2'=>array( self::BELONGS_TO, 'teachers2', 'tid' ),
);
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'tid' => 'Tid',
'status' => 'Status',
'status_date' => 'Status Date',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->with = array( 'teachers2' );
$criteria->compare( 'user_fname', $this->search_name, TRUE );
$criteria->compare('id',$this->id);
$criteria->compare('tid',$this->tid);
$criteria->compare('status',$this->status,true);
$criteria->compare('status_date',$this->status_date,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'author_search'=>array(
'asc'=>'teachers2.user_fname',
'desc'=>'teachers2.user_fname DESC',
),
'*',
),
),
));
}
public static function get_teacher_name($id){
$con = Yii::app()->db;
$query = $con->createcommand("SELECT concat(user_fname,' ',user_lname) as name FROM user_account WHERE user_id = $id");
$count = $query->query();
if($count->rowCount>0){
$wew = $query->queryall();
return $wew[0]['name'];
} else {
return null;
}
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
my gridview
array(
'type'=>'raw',
'name'=>'search_name',
'value' => 'CHtml::value($data,"teachers2.user_fname")',
'filter' => CHtml::activeTextField($model, 'search_name'),
),
filter search_name is not working.
any idea why?
-Juliano[/font]