0

I am using yii2 framework and trying to join multiple tables.

I have managed to join 3 tables together but i am not clear how to extend this to 4 tables.

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',
        ],
        ....

So that works fine, however i now want to join an additional table related to locations. the join would be locations.task_group_id = task_group.id. Full join as follows

**** I succeeded with this above ****

  1. responses.task_id = tasks..id

  2. tasks.location_id = locations.id

  3. locations->name (name being the field in the locations table)

How do I do this?

  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 being the field in the task_group table)

Richard perris
  • 511
  • 1
  • 6
  • 15

1 Answers1

0

Done it, Heres how.

I did this in the view

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

and it worked

****** My Steps ********

in Tasks Model

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

In Locations Model

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

public function getTaskOwner()
{
    return $this->hasOne(TaskOwner::className(), 
      ['id' => 'task_owner_id']);
}

In This Model

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

Then in this view grid view widget

  ....
 'columns' => [
 ....
[
  'class' => 'kartik\grid\DataColumn',
  'value'=> 'tasks.location.taskowner.name',
  .....
],
.....
Richard perris
  • 511
  • 1
  • 6
  • 15