Filter To See Data

hi…

I already made a list in my system…

for example: LIST OF APPLICATIONS

for sure, admin can see all the list but user can only see his/her own data…

anyone know how to make the user see their own data?

when I ask somebody, he ask me to filter it, but I don’t get how to filter it…

anyone can help me?

Below code is from my site, hope it can bring some ideas for you.

I have list for insurance form, and each customer can only see his own insurance form. But Agent can see all his submitted customer forms.

It’s actionList in the controller.




	/**

	 * Lists all models.

	 */

	public function actionList()

	{

		if(Yii::app()->user->role == "customer")

		{

			$dataProvider=new CActiveDataProvider('InsuranceForm',

				array('criteria'=>array(

						'condition'=>'customer_user_id='.Yii::app()->user->id,

						'order'=>'insurance_form_id DESC',

					),

					'countCriteria'=>array(

						'condition'=>'customer_user_id='.Yii::app()->user->id,

					),

					'pagination'=>array(

						'pageSize'=>5,

					),

				)

			);

		}

		elseif(Yii::app()->user->role == "agent")

		{

			$dataProvider=new CActiveDataProvider('InsuranceForm',

				array('criteria'=>array(

						'condition'=>'agent_user_id='.Yii::app()->user->id,

						'order'=>'insurance_form_id DESC',

					),

					'countCriteria'=>array(

						'condition'=>'agent_user_id='.Yii::app()->user->id,

					),

					'pagination'=>array(

						'pageSize'=>20,

					),

				)

			);			

		}

		$this->render('list',array(

				'dataProvider'=>$dataProvider,

		));

	}



hi Johnny…thank you for your feedback! :)

I already try to do like what you already give to me, but it’s still no working for me…

when I do like this, it produce error “UNDEFINED: $dataProvider” if I’m not mistaken

here, I give you my original list coding before I changed it like yours…

hope you don’t mind to check it…:)

this is in controller…


public function actionSenaraiPermohonan()

	{

		$model=new Permohonan;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Permohonan']))

		{

			$model->attributes=$_POST['Permohonan'];

			if($model->save())

				$this->redirect(array('permohonan/senaraiPermohonan'));

		}


		$this->render('senaraiPermohonan',array(

			'model'=>$model,

                    

		));

	}

and this is the like "form" list…


<?php

/* @var $this PermohonanController */

/* @var $model Permohonan */


/*$this->breadcrumbs=array(

	'Permohonan'=>array('index'),

	'Manage',

);//*/


$this->menu=array(

	array('label'=>'List Permohonan', 'url'=>array('index')),

        array('label'=>'Manage Permohonan', 'url'=>array('admin')),

);


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$('#permohonan-grid').yiiGridView('update', {

		data: $(this).serialize()

	});

	return false;

});

");

?>


<h1>Senarai Permohonan</h1>




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'permohonan-grid',

	'dataProvider'=>$model->search(),

	//'filter'=>$model,

	'columns'=>array(

            

            array(

                'header'=>'Nama Pegawai',

                'value'=>'$data->pegIc->peg_nama'),             

             array(

                'header'=>'Negara yang Dilawati',

                'value'=>'$data->neg->neg_lawat'),


             array(

                'header'=>'Tujuan Lawatan',

                'value'=>'$data->tuj->tuj_sbb'),

            array(

                'header'=>'Tarikh Mula Lawatan',

                'value'=>'$data->moh_cutimula'),

            array(

                'header'=>'Tarikh Tamat Lawatan',

                'value'=>'$data->moh_cutitamat'),

            array(

                'header'=>'Tarikh Permohonan',

                'value'=>'$data->moh_tarikh'),

            array(

                'header'=>'Status Permohonan',

                'value'=>'$data->stat->stat_status'),

           

        

		array(

                        'class'=>'CButtonColumn',

                        'template'=>' {update} ',

                    

                         'buttons'=>array(

				'update' => array(

                                    'url'=>'Yii::app()->controller->createUrl("pegawai/borang",(array("idtab"=>0,"peg_ic"=>$data->peg_ic,"moh_id"=>$data->moh_id)))',

				),

			),

		),

            

            array(

                        'class'=>'CButtonColumn',

                        'template'=>' {delete} ',

                        'visible' =>(Yii::app()->user->name=='admin'),

                ),

            ),

)); ?>




I hope you or anyone can check this for me…:)

Still cannot see what you where you limit the form list by current log-in user.

Where you got the Error "UNDEFINED: $dataProvider", mostly it is because your syntax mistake.