Yii2 Gridview table showing Data

yii2 gridview example
yii2 gridview row options
yii2 gridview relational data
yii2 gridview filter
yii2 gridview custom column
yii2 gridview filter example
yii2 listview
yii2 detailview

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!


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,
            ]);

GridView, yii\grid\GridView | API Documentation for Yii 2.0, The GridView widget is used to display data in a grid. The columns of the grid table are configured in terms of yii\grid\Column classes, which are configured via​  Yii2 GridView Showing data from multiple tables. Ask Question Asked 1 year, 6 months ago. Active 1 year, 6 months ago. Yii2 Gridview table showing Data.


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.

Yii2 Gridview table showing Data, If you are using UserModel , just replace your query in SearchModel : $query->​andFilterWhere ([ 'user_partner_id'  Thanks for watching Yii2 Tutorial 11 Searching Related Table Data From the GridView. Stay tuned for more tutorials. Have fun with Yii Support me - https://ww


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.

Rendering Data in Yii 2 with GridView and ListView, If you are using UserModel , just replace your query in SearchModel : $query->​andFilterWhere ([ 'user_partner_id' => Yii::$app->user->identity->sess_id_user,  Yii provides two components to visualize the data put into the data provider. The GridView will put the data in an HTML table. The ListView allows you to specify a partial view with which you can render each of the models in the data provider in a specific way.


Yii - GridView Widget, I won't be using the ArticlesTags table since it is just used for an N:M relation. Of course, there is a model available for it which is used in the  The GridView widget is used to display data in a grid. It provides features like sorting , paging and also filtering the data. A basic usage looks like the following:


Output data widgets, Each row of the table represents a single data item, and a column represents an attribute of the item. Step 1 − Modify the datawidget view this way. <?php use yii\​  GridView ¶ Data grid or GridView is one of the most powerful Yii widgets. It is extremely useful if you need to quickly build the admin section of the system. It takes data from a data provider and renders each row using a set of columns presenting data in the form of a table.


Yii2 gridview custom column, Data grid or GridView is one of the most powerful Yii widgets. each row using a set of columns presenting data in the form of a table. Or simply use the getData() function on the dataprovider object to get an array of CActiveRecords. Then use a foreach loop to display the data howsoever you pleases.