2017-09-05 108 views
0

我在模型Yii2框架表連接

public function getApplication() 
{ 
    return $this->hasOne(Applications::className(), ['application_id' => 'application_id']); 
} 

public function getAccount() 
{ 
    return $this->hasOne(Accounts::className(), ['account_id' => 'account_id']); 
} 

public function getUser() 
{ 
    return $this->hasOne(Users::className(), ['user_id' => 'oncall_id']); 
} 

並在視圖定義如下關係:

GridView::widget([ 
'dataProvider' => $dataProvider, 
'tableOptions' => ['class' => 'table table-bordered table-hover table-striped'], 
'columns' => [ 
    ['attribute' => 'oncalls.start_date', 'value' => 'start_date', 'headerOptions' => ['width' => '10%']], 
    ['label' => 'Shift', 'value' => function($data) { 
      if ($data->shift_start && $data->shift_end) 
       return $data->shift_start . " -> " . $data->shift_end; 
     }], 
    'account.account_name', 
    'application.application_name', 
    'user.real_name', 
    ['label' => 'Priority', 'value' => function ($dataProvider) { 
      if ($dataProvider->priority == 1) 
       return "Primary"; 
      else 
       return "Secondary"; 
     }], 
    'user.phone_1', 
    'user.phone_2', 
], 

]);

但是,在調試器中,似乎有過多的查詢正在進行。 對於每個分配記錄,有一個適當的表(用戶,應用程序,帳戶)的選擇查詢

我該如何避免有這些查詢,只是有一個與所有這些值的聯合聲明?

+0

ü可以編輯dataProvider中傳遞到GridView控件在searchmodel – mohsen

+0

@mohsen,你的意思編輯的數據提供程序,在關係模型中的設定是什麼,你能不能舉個例子? – Dunams

回答