phpyii

Custom views for cgridview based on role


I want to filter my CgridView to only display has "student" role from Authassignment Model, I read some tutorials but none of them show this. Please help me, this is related to my thesis.

<?php 

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

        'userID',
        'lastname',

        array(
            'class'=>'bootstrap.widgets.TbButtonColumn',
        ),
    ),
)); 

?>

 

StudentView


Solution

  • I don't know what's your table structure is but the best way of doing this may be as follows. Create a field in user table named user_role and at the time of user creation insert value in this like 1 for admin 2 for student and then you can easily get only student role by below method:

    public function actionIndex()
        {
            $model=new User('search');
                    
            $model->unsetAttributes();  // clear any default values
            $model->user_role = 2;// for student role 
            if(isset($_GET['User']))
                $model->attributes=$_GET['User'];
     
            $this->render('index',array(
                'model'=>$model,
            ));
        }