2017-02-28 4 views
0

Ich benutze yii2 Framework und versuche, mehrere Tabellen zu verbinden.Mehrere Tabellen in GridView verbinden

Ich habe es geschafft, 3 Tabellen zusammen zu verbinden, aber ich bin nicht klar, wie Sie das auf 4 Tabellen erweitern.

I joined three tables as follows >>> 

//In Tasks Model 
public function getLocation() 
{ 
    return $this->hasOne(Locations::className(), 
    ['id' => 'location_id']); 
} 

//in current Model 
public function getLocation() 
{ 
    return $this->hasOne(Tasks::className(),['id'=>'task_id']) 
     ->with(['location']); 
} 


//then in grid view 
.... 
'columns' => [ 
    [ 
     .... 
     [ 
      class' => 'kartik\grid\DataColumn', 
      'label' => 'Name', 
      'value' => 'tasks.location.name', 
     ], 
     .... 

Also das funktioniert gut, aber ich möchte jetzt eine zusätzliche Tabelle im Zusammenhang mit Standorten beitreten. Der Join wäre locations.task_group_id = task_group.id. Voll verbinden wie folgt

**** Ich konnte mit über diesem ****

  1. responses.task_id = tasks..id

  2. tasks.location_id = locations.id

  3. locations-> Name (Name das Feld in der Tabelle Orten sein)

Wie mache ich das?

  1. responses.task_id = tasks..id

  2. tasks.location_id = locations.id

  3. locations.task_group.id = task_group.id

  4. task_group-> name (Name ist das Feld in der Tabelle task_group)

Antwort

0

Fertig, Heres wie.

Ich tat dies in der Ansicht

.... 
'columns' => [ 
.... 
[ 
    'class' => 'kartik\grid\DataColumn', 
    'value'=> 'tasks.location.taskowner.name', 
    ..... 
], 
..... 

und es funktionierte

****** Meine Schritte ********

in Aufgaben Modell

public function getLocation() 
{ 
    return $this->hasOne(Locations::className(), 
    ['id' => 'location_id']); 
} 

In den Positionen Modell

In diesem Modell

public function getTasks() 
{ 
    return $this->hasOne(Tasks::className(), ['id' => 'task_id']); 
} 

Dann in dieser Ansicht Rasteransicht Widget

.... 
'columns' => [ 
.... 
[ 
    'class' => 'kartik\grid\DataColumn', 
    'value'=> 'tasks.location.taskowner.name', 
    ..... 
], 
..... 
Verwandte Themen