我有 3 张桌子
“设置”表的架构如下。
id | name
1 | aaa
2 | bbb
请求表如下
id| request_no| request_type_id (id of setup table)
1 | SM000001 | 1
2 | SM000002 | 2
这是历史表
id | request_id | status | date
1 | 1 | Pending | 2013-07-04 14:39:03
2 | 1 | Reviewing | 2013-07-05 01:10:14
3 | 1 | Implementing | 2013-07-06 11:25:54
4 | 1 | Completed | 2013-07-07 12:36:32
5 | 2 | Pending | 2013-07-10 15:05:56
6 | 2 | Reviewing | 2013-07-11 03:08:04
7 | 2 | Implementing | 2013-07-13 11:45:48
8 | 2 | Completed | 2013-07-17 14:28:15
在gridview我想显示为
request no | request type
SM000004 | aaa
SM000006 | bbb
这是网格视图
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'request-processing-time-grid',
'dataProvider'=>$report->newsearch(),
'filter'=>$report,
'afterAjaxUpdate' => 'reinstallDatePicker', // (#1)
'columns'=>array(
'request_no',
array(
'name' => 'request_type_id',
//'value' => '(in_array($data["request_type_id"], $types) ? $data->requesttypes->name : "1")',
//'value' => '$data["request_type_id"]',
//'value' => $types['$data["request_type_id"]'],
//'value' => '$data->requesttypes->name',
//'value' => '$data["request_type_id"]',
'value' => '$data["request_type_id"].requesttypes.name',
'filter' => $types,
),
),
));
这是模型的新搜索
public function newsearch(){
$query = "SELECT a.request_id, r.request_no, r.request_type_id, r.request_category_id, r.financial_type_id, r.urgency_id,
CONCAT(
FLOOR(HOUR(TIMEDIFF(implementing,reviewing)) / 24), ' days ',
MOD(HOUR(TIMEDIFF(implementing,reviewing)), 24), ' hours ',
MINUTE(TIMEDIFF(implementing,reviewing)), ' minutes') rT,
CONCAT(
FLOOR(HOUR(TIMEDIFF(completed,implementing)) / 24), ' days ',
MOD(HOUR(TIMEDIFF(completed,implementing)), 24), ' hours ',
MINUTE(TIMEDIFF(completed,implementing)), ' minutes') iT
FROM (
SELECT x.request_id
, MAX(CASE WHEN status = 'pending' THEN created_date END) pending
, MAX(CASE WHEN status = 'reviewing' THEN created_date END) reviewing
, MAX(CASE WHEN status = 'implementing' THEN created_date END) implementing
, MAX(CASE WHEN status = 'completed' THEN created_date END) completed
FROM history x
GROUP
BY request_id
) a, request r WHERE r.id = a.request_id ";
$count=Yii::app()->db->createCommand($query)->queryScalar();
$key="request_id";
$dataProvider=new CSqlDataProvider($query, array(
'totalItemCount'=>$count,
'keyField' => $key,
'sort'=>array(
'attributes'=>array(
'request_id' ),
),
'pagination'=>array(
'pageSize'=>10,
),
));
return $dataProvider;
}
这是表的关系
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'requesttypes' => array(self::BELONGS_TO, 'Setup', 'request_type_id'),
'requestcategories' => array(self::BELONGS_TO, 'Setup', 'request_category_id'),
'requestfinanicaltypes' => array(self::BELONGS_TO, 'Setup', 'financial_type_id'),
'requesturgent' => array(self::BELONGS_TO, 'Setup', 'urgency_id'),
'profiles' => array(self::BELONGS_TO, 'Profiles', 'user_id'),
'requests' => array(self::BELONGS_TO, 'Request', 'request_id'),
);
}
请求类型仅显示request_type_id
. 我想显示 `setup.name 的名称。
我怎样才能做到这一点?