cpass 2012-05-14 17:26:01 4079次浏览 5条回复 0 0 0

一个用户相关的管理页面使用默认的CGridView,其中

'columns'=>array(
  'goods_id',
  array(
    'name'=>'user_id',
    'value'=>'User::model()->findByPk($data->user_id)->name',
    //'filter'=>''
  ),
  ...
)

这里将用户ID(user_id)显示成了用户姓名(name),但是上方的搜素框中搜索的还是用户ID,怎么让它搜索用户名呢(name),就是这个filter咋整? 没思路啊

  • 回复于 2012-05-14 19:56 举报

    我觉得这个能帮到你:
    http://www.yiichina.com/topic/2436

  • 回复于 2012-05-15 09:56 举报

    唉,看到过,没看懂,看来还得努力啊 💫

  • 回复于 2012-11-04 18:39 举报

    那自己改一下就是了,关键是你改了,点击搜索按钮无效果,我也正在在纳闷中。

  • 回复于 2012-11-07 16:00 举报

    BoyLee 的视频里有

  • 回复于 2013-01-03 14:59 举报

    Searching and Sorting by Count of Related Items in CGridView
    http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview/
    在GridView中使用关联模型进行搜索和排序

    首先我们有两个模型它们直接有关联:

    class Author extends CActiveRecord {
    ...
    }
     
    class Post extends CActiveRecord {
    ...
        function relations() {
            return array(
                'author'=>array( self::BELONGS_TO, 'Author', 'id_author' ),
            );
        }
    ...
    }
    

    当以网格形式显示所有 Post 时,我们希望显示作者的名字,并且可以通过作者名字中的关键字过滤 Post。提供这些功能的最好解决方式(在我看来)是:

    首先需要在 Post 模型中添加一个新的属性,它用来保存搜索的字符串(即要搜索的作者名).也可以使用外键列来实现同样的效果,但是我不喜欢这么用,在搜索条件中保存搜索的字符串而不是外键 id.你还须在搜索条件中将这个新的属性的规则设置为 safe。

    class Post extends CActiveRecord {
      public $author_search;
      ...
      public function rules() {
        return array(
          ...
          array( 'xxx,yyy,author_search', 'safe', 'on'=>'search' ),
        );
      }
    }
    

    现在就可以在搜索条件(标准情况-每个模型都要一个 search 方法)中使用这个属性了。同时,我们需要使用条件的 ‘with’ 属性来指定我们的 Post 是通过哪个关系来获取作者(这种方式只需一次数据库查询而不是延迟加载中的多次查询)。

    $criteria = new CDbCriteria;
    $criteria->with = array( 'author' );
    ...
    $criteria->compare( 'author.username', $this->author_search, true );
    ...当我们修改搜索函数时,我们对返回的 CActiveDataProvider 添加一个新的功能
    
    return new CActiveDataProvider( 'Post', array(
        'criteria'=>$criteria,
        'sort'=>array(
            'attributes'=>array(
                'author_search'=>array(
                    'asc'=>'author.username',
                    'desc'=>'author.username DESC',
                ),
                '*',
            ),
        ),
    ));
    

    配置中排序部分的 attributes 允许我们覆盖默认值。当我们按 author_search 字段排序的时候,它将会按照指定的规则排序,最后的 * 表示其他字段按默认排序。通过这种方式我们也可以修改默认属性的排序(例如:用户指定按 last_name 列排序时,应该使用last_name和first_name结合排序).

    到现在为止我们已经为我们的网格显示做好了前期准备

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns'=>array(
            'title',
            'post_time',
            array( 'name'=>'author_search', 'value'=>'$data->author->username' ),
            array(
                'class'=>'CButtonColumn',
            ),
        ),
    ));
    

    这就是所有,我们使用用户名代替用户ID外键列来排序,并且我们可以使用姓名关键字搜索.

您需要登录后才可以回复。登录 | 立即注册