【发布时间】:2013-04-12 16:09:21
【问题描述】:
我在 CGrid 中使用 `` 页面中的关系模型对关系数据进行排序时遇到问题。
简述我的场景:
我有一个用户 model: Entities=> id,username
还有一个个人资料 Model: Entities=> id, firstname,lastname, user_id,etc..
我想在user model 中列出user model 中的profile model 和username CGrid,以便排序和搜索 很好。在我的例子中,username 的排序是由 user_id 完成的,而不是由 username 完成的。我想通过username搜索它,所以我做了以下,
我的控制器动作:
$model = new Profile('search');
$model -> unsetAttributes();// clear any default values
if (isset($_GET['Profile']))
$model -> attributes = $_GET['Profile'];
$this -> render('MyPage', array('model' => $model ));
我的模特关系:
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'user', 'user_id'),);
}
示范规则:
array( 'xxx,yyy,user_name', 'safe', 'on'=>'search' ),
及模型搜索功能
if(!empty($this->user_id)){
$criteria->with='user';
$criteria->order = ::app()->request->getParam('sort');// 'username ASC'
}
$criteria -> compare('user.username', $this->user_id, true);
我的
$this->widget('zii.widgets.grid.CGrid', array(
'id'=>'profile-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
array('name'=>'user_id',
'header'=>User::model()->getAttributeLabel('username'),
'value' =>'$data->getRelated(\'user\')->username',
'type'=>'raw',
'htmlOptions'=>array('style'=>'text-align: center'),),
---------------
在排序期间,排序工作完美,但排序是基于user_id 而不是username。我缺少这样做的任何东西。请提出建议。
参考:Here(我也尝试过声明一个公共变量作为链接中的建议但机器人工作g。)
编辑:问题修复后。 也感谢this 链接。
【问题讨论】:
标签: yii cgridview yii-events