I have two tables:
post [id, title, content, img, user_id]
user [id, username, email, password]
How can use filter user_id and username in CGridView?
I added username with filter username but I need add user_id with filter user_id too.
Post Model:
Public $username;
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
);
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('title',$this->title,true);
$criteria->compare('content',$this->content,true);
$criteria->compare('img',$this->img,true);
$criteria->with=array('user');
$criteria->addSearchCondition('user.username',$this->user_id,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Post view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'type-post-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'title',
'content',
'img',
'user_id'=>array(
'name'=>'user_id',
'value'=> '$data->user->username',
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>