kusmi
kusmi

Reputation: 1

how to join table in grid view in yii2

i want to join my company table name to employee id. here my code in employee model

   public function attributeLabels()
        {
            return [
               'id' => 'ID',
                //'company_id' => 'Company ID',
                'emp_name' => 'Emp Name',
                'emp_email' => 'Emp Email',
                'emp_salery' => 'Emp Salery',
            ];
        }

       public function getCompany()
       {
       return $this->hasOne(Company::className(),['id' => 'company_id']);

       }

in index file i use this code

    <?php PJax::begin();?>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                //['class' => 'yii\grid\SerialColumn'],

                //'id',
                [
              'attribute' => 'company_id',
              'value' => 'company.name',
             ],
                'emp_name',
                'emp_email:email',
                'emp_salery',

                ['class' => 'yii\grid\ActionColumn'],
            ],
        ]); ?>
    <?php PJax::end();?></div>

in employee search table i use this code

    public function search($params)
        {
            $query = Employee::find();
          $query->joinWith(['company']);
            // 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;
            }

            // grid filtering conditions
            $query->andFilterWhere([
                'id' => $this->id,
            ]);

            $query->andFilterWhere(['like', 'company_id', $this->company_id])
                ->andFilterWhere(['like', 'emp_name', $this->emp_name])
                ->andFilterWhere(['like', 'emp_email', $this->emp_email])
                ->andFilterWhere(['like', 'emp_salery', $this->emp_salery])
                ->andFilterWhere(['like', 'Company.name', $this->company_id])
             ->andFilterWhere(['like', 'company.name', $this->company])
    ;


            return $dataProvider;
        }

http://localhost/test/web/employee/

is shows in company field (not set) pls help me

Upvotes: 0

Views: 1093

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

You should add a getter eg:getCompanyname()n your base model

    ......
    public function attributeLabels()
    {
        return [
           'id' => 'ID',
            //'company_id' => 'Company ID',
            'emp_name' => 'Emp Name',
            'emp_email' => 'Emp Email',
            'emp_salery' => 'Emp Salery',
            'companyName' =>  'Company Name')
        ];
    }

   public function getCompany()
   {
   return $this->hasOne(Company::className(),['id' => 'company_id']);

   }

You should add a getter for company name

   /* 
      Getter for company name 

   */
  public function getCommpanyName() {
      return $this->company->name;
  }

and in your gridview you should use the new get name (companyName)

  <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [

            'companyName',

            'emp_name',
            'emp_email:email',
            'emp_salery',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

see this for more complete samples http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

Upvotes: 0

mohammad zahedi
mohammad zahedi

Reputation: 323

you can do this in two difference way : 1 . If you want use active record you must set property in first model you want to select it and join table with that like:

public $pin;

then when you get data you can access to pin in second table

the second way is you use asArray() that return all off data in both table $model = News::find()->leftJoin('comment', 'news.id = comment.news_id')->asArray()->all(); then you can use it in your gridview

Upvotes: 1

Related Questions