1

我有 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 的名称。

我怎样才能做到这一点?

4

2 回答 2

0

这里我举个例子:

你有两张桌子:

Setup: id, name
Request: id, request_type_id, request_no

在关系中,您可以定义:

'setup'    => array(self::BELONGS_TO, 'Setup',    'request_type_id'),

然后在搜索的时候

$requests = Request::model()->with('setup')->findAll();

或者,如果您需要任何条件,您可以在这里申请。

然后您可以为 $requests 中的每个 $request 访问这样的字段:

$setup_name = $request->setup->name;

如果您需要使用 dataProvider,您可以执行以下操作:

  $criteria = new CDbCriteria;
  $criteria->with = array('setup');

  $dataProvider=new CActiveDataProvider(Request, array(
            'criteria'=> $criteria,
            ....
    ));

然后在你的gridview中你可以拥有:

$data->setup->name
于 2013-08-06T09:13:02.013 回答
0

这种模型的新搜索

public function newsearch(){
        $query = "SELECT a.request_id, r.request_no, s1.name request_type, s2.name request_category, s3.name financial_type, s4.name request_urgency,
            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 LEFT JOIN setup s1 ON r.request_type_id = s1.id
            LEFT JOIN setup s2 ON r.request_category_id = s2.id
            LEFT JOIN setup s3 ON r.financial_type_id = s3.id
            LEFT JOIN setup s4 ON r.urgency_id= s4.id
            WHERE r.id = a.request_id ";

        if(isset($this->request_no))
            $query .= " AND r.request_no LIKE '%" . $this->request_no ."%'";

        if(isset($this->request_type_id))
            $query .= " AND r.request_type_id LIKE '%" . $this->request_type_id ."%'";

        if(isset($this->request_category_id))
            $query .= " AND r.request_category_id LIKE '%" . $this->request_category_id ."%'";

        if(isset($this->urgency_id))
            $query .= " AND r.urgency_id LIKE '%" . $this->urgency_id ."%'";

        //echo $query;
        //die;
        $count=Yii::app()->db->createCommand($query)->queryScalar();
        $key="request_id";
        $dataProvider=new CSqlDataProvider($query, array(
                'totalItemCount'=>$count + 1,
                //'with'=>array('requesttypes'),
                'keyField' => $key,
                'sort'=>array(
                        'attributes'=>array(
                                'request_id'                ),
                ),
                'pagination'=>array(
                        'pageSize'=>10,
                ),
        ));

        return $dataProvider;
    }

这是视图的cgridview

<?php 
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'request-processing-time-grid',
    'dataProvider'=>$report->newsearch(),
    'filter'=>$report,
    'afterAjaxUpdate' => 'reinstallDatePicker', // (#1)
    'columns'=>array(
        'request_no',
        //'request_id',
        array(
            'name'      => 'request_type_id',
            'value'     => '$data["request_type"]',
            'filter'    => $types,
        ),
        array(
            'name'      => 'request_category_id',
            'value'     => '$data["request_category"]',
            'filter'    => $categories,
        ),
        array(
            'name'      => 'urgency_id',
            'value'     => '$data["request_urgency"]',
            'filter'    => $urgencies,
        ),  
        array(
            'header'    => 'Time in Reviewing',
            'name'      => 'rT',
            'value'     => '($data["rT"] == NULL) ? "Not Revewing" : $data["rT"]',  
            'filter'    => '',
            ),
        array(
            'header'    => 'Time in Implementing',
            'name'      => 'iT',
            'value'     => '($data["iT"] == NULL) ? "Not Implementing" : $data["iT"]',
            'filter'    => '',
        ),

        ),

    ));     
?>
于 2013-08-07T05:14:15.313 回答