我有一个搜索模型:
$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;
}
],
它说获取未知属性:common \ models \ User :: my_orders_count
请帮助访问此字段
答案 0 :(得分:2)
您可以为计算字段
执行此操作在用户模型中添加公共变量
public user_count;
在选择
中使用别名.....
$myOrdersQuery = Request::find()
->select('count(*) as user_count')
->where([
....
然后简单地引用gridview中的公共字段名称
[
'attribute' => 'user_count',
],