2016-04-01 73 views
1

我有一個搜索模式:Yii2。如何訪問搜索模型中的子查詢字段?

$query = User::find(); 
$myOrdersQuery = Request::find() 
          ->select('count(*)') 
          ->where([ 
           'user_id' => $clientsIds, 
           'agent_id' => $this->viewer->id, 
          ]); 
$query->addSelect(['my_orders_count' => $myOrdersQuery]); 

當我試圖把它打印到GridView控件

 [ 
      'attribute' => 'my_orders_count', 
      'value' => function ($model) { 
       return $model->my_orders_count; 
      } 
     ], 

它說,獲得未知屬性:共同\型號\用戶:: my_orders_count

請幫忙查看這個字段

回答

2

你可以像計算字段那樣做

廣告公共變種在用戶模式

public user_count; 

使用別名在選擇

..... 
$myOrdersQuery = Request::find() 
         ->select('count(*) as user_count') 
         ->where([ 
         .... 

然後簡單地指公共字段名稱的GridView

[ 
     'attribute' => 'user_count', 

    ], 
+0

@Maxim歡迎您 – scaisEdge