doukunsan5553 2010-03-21 11:24
浏览 82
已采纳

在我构建使用关系表构建的自定义关键字搜索后,排序和分页不起作用

I recently started to build a custom keyword search using Yii 1.1.x

The search works 100%. But as soon as I sort the columns and use pagination in the admin view the search gets lost and all results are shown. So with otherwords it's not filtering so that only the search results show. Somehow it resets it.

In my controller my code looks as follows

$builder=Messages::model()->getCommandBuilder();

        //Table1 Columns
        $columns1=array('0'=>'id','1'=>'to','2'=>'from','3'=>'message','4'=>'error_code','5'=>'date_send');

        //Table 2 Columns
        $columns2=array('0'=>'username');

        //building the Keywords
        $keywords = explode(' ',$_REQUEST['search']);
        $count=0;
        foreach($keywords as $key){
            $kw[$count]=$key;
            ++$count;
        }   

        $keywords=$kw;

        $condition1=$builder->createSearchCondition(Messages::model()->tableName(),$columns1,$keywords,$prefix='t.');
        $condition2=$builder->createSearchCondition(Users::model()->tableName(),$columns2,$keywords);
        $condition = substr($condition1,0,-1) . " OR ".substr($condition2,1);
        $condition = str_replace('AND','OR',$condition);


$dataProvider=new CActiveDataProvider('Messages', array(
                'pagination'=>array(
                    'pageSize'=>self::PAGE_SIZE,
                ),
                'criteria'=>array(
                    'with'=>'users',
                    'together'=>true,
                    'joinType'=>'LEFT JOIN',
                    'condition'=>$condition,
                ),

                'sort'=>$sort,
            ));

$this->render('admin',array(
            'dataProvider'=>$dataProvider,'keywords'=>implode(' ',$keywords),'sort'=>$sort
        ));

and my view looks like this

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'id',
        array(
            'name'=>'user_id',
            'value'=>'CHtml::encode(Users::model()->getReseller($data->user_id))',
            'visible'=>Yii::app()->user->checkAccess('poweradministrator')
        ),
        'to',
        'from',
        'message',
        /*
        'date_send',
        */
        array(
            'name'=>'error_code',
            'value'=>'CHtml::encode($data->status($data->error_code))',
        ),
        array(
            'class'=>'CButtonColumn',
            'template'=>'{view} {delete}',
        ),


    ),

));

I really do not know what do do anymore since I'm terribly lost, any help will be hihsly appreciated

  • 写回答

2条回答 默认 最新

  • duanduan8439 2010-09-01 00:40
    关注

    You could set a user state for your search criteria and test for the state each time the controller loads your view.

    Something along the lines of

    if(isset($_REQUEST['search'])){
        $keywords = explode(' ',$_REQUEST['search']);
        Yii::app()->user->setState('keywords',$keywords);
    }
    else if(Yii::app()->user->hasState('keywords')){
        $keywords=Yii::app()->user->getState('keywords');
    }

    The drawback here is the keywords state will be maintained for the length of the session.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料