tags:

views:

451

answers:

2

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

A: 

that's why I dont' like these frameworks. In raw PHP I'd make it with just $link=http_build_query($_GET); and then use this link for both pagination and sorting. But you have to find a way to do the same using your framework ideology. I bet they have some example for such a commonplace task.

Col. Shrapnel
+1  A: 

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.

Jason George