anthony.gough Posted February 2, 2014 Share Posted February 2, 2014 The problem I have is that I am returning a subset of data from the User table to a view. Using TbGridView I should be able to type in a search term and this data should be filtered by my search term however the data is not filtered. The same data is simply retuned. The filtering is not working. I have an action in my User controller to show a list of users that have previously logged in. The action is public function actionLoginhistory() { $model=new User('searchlogin'); $model->unsetAttributes(); // clear any default values if(isset($_GET['User'])) { $model->attributes=$_GET['User']; } $this->render('loginlist', array( 'model'=>$model, )); } This returns the correct data set to my view. The problem is that when I try to filter the data by typing in a search term in the Trading Name column or Email column the same data set is returned. The view code is: $this->widget('bootstrap.widgets.TbGridView', array( 'type' => 'striped bordered condensed', 'dataProvider' => $model->searchlogin(), 'filter'=>$model, 'ajaxUpdate'=>false, 'enablePagination'=>true, 'summaryText'=>'Displaying {start}-{end} of {count} results.', 'template' => "{summary}{items}{pager}", 'htmlOptions'=>array('style' => 'width:500px;margin-left: 130px;',), 'columns' => array( array('name' => 'real_name', 'header'=> 'Trading Name','htmlOptions'=>array('width'=>'50%'), 'type'=>'raw', 'value' =>'CHtml::link($data->real_name, array("contractors/viewalldocuments","id"=>$data->username))', ), array('name' => 'email', 'header' => 'Email','htmlOptions'=>array('width'=>'30%')), array('name' => 'last_login', 'header' => 'Last Login', 'filter'=>false, 'htmlOptions'=>array('width'=>'20%'), 'value' => 'date("d-m-Y", strtotime($data->last_login))'), )) The data set returned is from the User model - the code for this is: public function searchlogin() { $criteria=new CDbCriteria; $criteria->compare('real_name', $this->real_name); $criteria->condition = 'last_login IS NOT NULL AND user_type_id=4'; return new CActiveDataProvider('User', array( 'pagination' => array( 'pageSize' => 80, ), 'criteria'=>$criteria, )); } If I change my view code to use the standard search function then every user in the user table is returned and the data will filter down by search term. So if I change one line in the view to 'dataProvider' => $model->search(), Then every user is returned but the data will filter by search term. The standard search function in the model this refers to in the User Model is public function search() { $criteria=new CDbCriteria; $criteria->compare('real_name',$this->real_name,true); $criteria->compare('username',$this->username,true); $criteria->compare('email',$this->email,true); $criteria->compare('last_login',$this->last_login,true); return new CActiveDataProvider($this, array( 'pagination' => array( 'pageSize' => 80, ), 'criteria'=>$criteria, )); } This should be really straight forward and simple. You should be able to have multiple search functions in a Model - I have been looking at this for days and I just cannot resolve the problem. I would appreciate any help. When I type in the search term the code generates what I would expect the url to be index.php?r=User%2Floginhistory&User%5Breal_name%5D=test&User%5Bemail%5D=&User_page=1 Link to comment Share on other sites More sharing options...
Recommended Posts