简体   繁体   English

如何在yii2的网格视图中联接表

[英]how to join table in grid view in yii2

i want to join my company table name to employee id. 我想将我的公司表名称加入员工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/ HTTP://本地主机/测试/网络/员工/

is shows in company field (not set) pls help me 是公司领域的节目(未设置)请帮助我

you can do this in two difference way : 1 . 您可以通过两种不同的方式执行此操作: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; 公开的$ 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(); 第二种方法是使用asArray()返回两个表中的所有数据$ model = News :: find()-> leftJoin('comment','news.id = comment.news_id')-> asArray()- >所有(); then you can use it in your gridview 然后可以在gridview中使用它

You should add a getter eg:getCompanyname()n your base model 您应该在基本模型中添加一个getter,例如:getCompanyname()

    ......
    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中,您应该使用新的获取名称(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/ 请参阅此以获得更完整的示例http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM