Ivan Rancic
Ivan Rancic

Reputation: 23

Yii2 Gridview table showing Data

my problem is next. I use the Yii2 gridview table and the user id I recorded in the session. And if I log in with the user who has for example ID 2, I want to display the data in the table only for the user with ID 2. Thanks for the answers in advance.

<?= GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],

    'company_name',
    'person_name',
    'company_address',
    'company_email:email',
    'meeting_date',
    'user_id',//external key
    'tel_fix',
    'tel_mob',

    ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update}'],
  ],
  'tableOptions' => ['class' => 'table table-striped table-bordered'],
]); ?>

Here is my actionIndex from PartnersController.

public function actionIndex()
    {
        $searchModel = new PartnersSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

And where can I do that?

Thank's a lot! But in your code, I made just one change and now it works great!

Instead of :

$query->andFilterWhere ( [
        'user_partner_id' => $this->user_partner_id,
      ] );

I wrote :

$query->andFilterWhere ( [
        'user_partner_id' => $_SESSION['sess_id_user'],
      ] );

Anyway, thank you very much!

Upvotes: 0

Views: 664

Answers (3)

akshaypjoshi
akshaypjoshi

Reputation: 1295

If you are using UserModel, just replace your query in SearchModel :


     $query->andFilterWhere ([
                'user_partner_id' => Yii::$app->user->identity->sess_id_user,
            ]);

Or if your sess_id_user is primary key in UserModel:


     $query->andFilterWhere ([
                'user_partner_id' => Yii::$app->user->id,
            ]);

Upvotes: 1

rob006
rob006

Reputation: 22174

You can add condition directly in controller action after creating $dataProvider:

public function actionIndex() {
    $searchModel = new PartnersSearch();

    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $dataprovider->query->andWhere(['user_partner_id' => $_SESSION['sess_id_user']]);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

Note that using andFilterWhere() in such cases may be dangerous - condition will be skipped if $_SESSION['sess_id_user'] is empty. So if user is a guest, it may see all the records if you not disallow guest access for this action.

Upvotes: 1

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23788

If you want to list only the records for the user that is logged in you need to do the following

In your controller/action add the following before you call the search() function from the model, i am assuming the name of the action is actionIndex() and the view name is index you can change them to your relative names.

You should not hardcode the user_partner_id in the query inside the search() function

 //THIS IS WRONG
 $query = Partners::find()->where('user_partner_id',$_SESSION['sess_id_user']);

but instead, you should pass it via params to the search() method as your PartnersSearch will be used by admins too which need to have all records accessible.

Change you controller/action

public function actionIndex(){
   $searchModel = new \common\models\PartnersSearch();
   $params = Yii::$app->request->queryParams;
   $params['PartnersSearch']['user_partner_id'] = Yii::$app->user->id;
   $dataProvider = $model->search ( $params );
   $this->render('index',['searchModel']);
}

and make sure you have the andFilterWhere condition added for the user_partner_id in the search() function of the CompanySearch model or add one before you return the $dataProvider in the search() method.

  public function search( $params ) {
        $query = PartnersSearch::find ();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider ( [
            'query' => $query ,
                ] );

        $this->load ( $params );

        if ( !$this->validate () ) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        //your rest of code
        .................

        /*THIS LINE NEEDS TO BE ADDED*/
        $query->andFilterWhere ( [
            'user_partner_id' => $this->user_partner_id,
        ] );
         /*THIS LINE NEEDS TO BE ADDED*/

         return $dataProvider;
}

EDIT

Make sure your field user_partner_id from the Partner model is added to the safe rules inside the PartnerSearch Model otherwise it wont load the value.

Upvotes: 1

Related Questions