【问题标题】:CGridView Sorting with relational table sorts by relaton Id parameterCGridView 排序与关系表按关系 Id 参数排序
【发布时间】: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


    【解决方案1】:

    嗯,你找到的 wiki 页面确实是一个好的开始......

    这是另一种方法:

    在您的个人资料模型中:

    // private username attribute, will be used on search
    private $_username;
    
    public function rules()
    {
        return array(
            // .....
            array('username', 'safe', 'on'=>'search'),
            // .....
        );
    }
    
    public function getUsername()
    {
        // return private attribute on search
        if ($this->scenario=='search')
            return $this->_username;
    
        // else return username
        if (isset($this->user_id)) && is_object($this->user))
            return $this->user->username;
    }
    
    public function setUsername($value)
    {
        // set private attribute for search
        $this->_username = $value;
    }
    
    public function search()
    {
        // .....
        $criteria->with = 'user'; 
        $criteria->compare('user.username', $this->username, true);
        // .....
    
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>array(
                'attributes'=>array(
                    'username'=>array('asc'=>'user.username', 'desc'=>'user.username DESC'),
                    '*',
                ),
            ),
        ));
    }
    

    在您看来,您应该尝试一下

    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'profile-grid',
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns'=>array(
            // .....
            'username',
            // .....
        ),
    );
    

    【讨论】:

    • 抱歉回复晚了。。当我尝试时,我得到以下信息:错误 500:

      CDbException

      CDbCommand 无法执行 SQL 语句:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user.username' in 'order Clause'

      我认为这行会抛出错误.. 'username'=>array('asc'=>'user.username', 'desc '=>'user.username DESC'),
    • 确保设置标准 with 参数:$criteria->with = 'user';
    • 是的,我做到了。正如你在我的问题中看到的那样。
    • if 声明中,就像您的问题一样?
    • 你让我到了那里..!终于修好了。
    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2015-04-22
    相关资源
    最近更新 更多