How to join three tables and get value in grid view

烂漫一生 提交于 2019-12-11 23:56:39

问题


I have three tables :

  1. contacts hasMany groups
  2. contact_groups hasMany contacts
  3. contact_contact_groups

columns in table contact

contact_id | contact_name

columns in table contact_groups

group_id | group_name

columns in table contact_contact_groups

contact_contact_group_id | contact_id | group_id

MODEL

contacs model

public function getContactContactGroups()
{
    return $this->hasMany(ContactContactGroups::className(),
                          ['contact_id' => 'contact_id']);
}

contact_groups model

public function getContactContactGroups()
{
    return $this->hasMany(ContactContactGroups::className(),
                          ['group_id' => 'group_id']);
}

contact_contact_groups model

public function getGroup()
{
  return $this->hasOne(ContactGroups::className(), ['group_id' => 'group_id']);
}

public function getContact()
{
  return $this->hasOne(Contacts::className(), ['contact_id' => 'contact_id']);
}

I want to display grid like this :

-----------------------------
Contact Name | Group Name
-----------------------------
Me           | Uncategorized
Mother       | Family
Jhon         | Business

VIEW

<?= GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'tableOptions' =>['class' => 'table table-striped table-bordered'],
  'columns' => [
    [
      'attribute' => 'contact_name',
      'value' => 'contact_name',
    ],
    [
      'attribute' => 'contactContactGroups.group_id',
      'value' => 'contactContactGroups.group.group_name',
      'filter' => Html::activeDropDownList($searchModel, 'group_id', ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(), 'group_id', 'group_name'),['class'=>'form-control','prompt' => 'Select Group']),
    ],
  ],]); 
?>

ContactsController

public function actionIndex() { 
    $this->unsetThisButton(array(4,5));   
    $searchModel = new ContactsSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
    return $this->render('index', [ 
          'searchModel' => $searchModel, 
          'dataProvider' => $dataProvider,
     ]); 
}

But it showing (not set) not a group_name .


回答1:


A simple way is based on adding a getter in your model eg: for contact_contact_groups model

you have a relation

public function getGroup()
{
  return $this->hasOne(ContactGroups::className(), ['group_id' => 'group_id']);
}

use a getter for group_name

 public function getGroup_group_name() {
    return $this->group->group_name;
}

and in grid view in the attribute

[
  'attribute' => 'group_group_name',
  'filter' => Html::activeDropDownList($searchModel, 'group_id', ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(), 'group_id', 'group_name'),['class'=>'form-control','prompt' => 'Select Group']),
],

do the same for the relation and field




回答2:


I Found simple stuff like this :)

GRID (VIEW)

[
        'attribute' => 'contactContactGroups.group_id',
        'value'=>function ($data) {
            $d = array();
            foreach ($data->contactContactGroups as $k=>$m)
            {
              $d[] = ContactContactGroups::get_group_name_by_id($m->group_id);
            }
            return implode($d, ', '); 
          },
        'filter' => Html::activeDropDownList($searchModel, 'group_id', ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(), 'group_id', 'group_name'),['class'=>'form-control','prompt' => 'Select Group']),
      ],

models/ContactContactGroups.php Model

I create function get_group_name_by_id($id)

public static function get_group_name_by_id($id){
  $model = ContactGroups::find()->where(["group_id" => $id])->one();
  if(!empty($model)){
    return $model->group_name;
  }
  return null;
}

so the result is :

Contact   | Category
-------------------------------
Me        | Business, Family
Erick     | Business, Office
Jhon      | Office

Thank's @scaisEdge, you give me some clue ;)



来源:https://stackoverflow.com/questions/35499252/how-to-join-three-tables-and-get-value-in-grid-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!