如何从YII中的CGridView的两个表中获取数据

时间:2014-01-23 17:17:43

标签: php yii

我有以下表格,

CREATE TABLE `m_user`(
    `user_id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_name` VARCHAR(50) NOT NULL,
    `pwd` VARCHAR(50) NOT NULL,
    `type` VARCHAR(25) NOT NULL,
    `actv_sw` VARCHAR(2) NOT NULL,
    `cre8_ts` TIMESTAMP NOT NULL,
    `lst_updt_ts` TIMESTAMP NOT NULL
);

CREATE TABLE `m_expense`(
    `expense_id` INT AUTO_INCREMENT PRIMARY KEY,
    `expense_name` VARCHAR(100) NOT NULL,
    `actv_sw` VARCHAR(2) NOT NULL,
    `added_dt` DATE NOT NULL,
    `cre8_ts` TIMESTAMP NOT NULL,
    `cre8_by` INT NOT NULL,
    `lst_updt_ts` TIMESTAMP,
    `lst_updt_by` INT,
    CONSTRAINT `m_expense_fk1` FOREIGN KEY (`cre8_by`)
        REFERENCES `m_user` (`user_id`),
    CONSTRAINT `m_expense_fk2` FOREIGN KEY (`lst_updt_by`)
        REFERENCES `m_user` (`user_id`)
);

和CGridview定义如下,

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'expense-grid',
    'emptyText' => 'No data Found',
    'dataProvider' => Expense::model()->search(),
    'cssFile' => Yii::app()->theme->baseUrl . '/css/gridView.css',
    'enablePagination' => true,
    'enableSorting' => true,
    'pagerCssClass' => 'pager',
    'columns' => array(
        //styling default buttons
        array(
            'header' => 'Update',
            'class' => 'CButtonColumn',
            'template' => '{update}',
            'updateButtonImageUrl' => Yii::app()->theme->baseUrl . '/images/' . 'gr-update.png'
        ),
        array(
            'name' => 'expense_id',
            'header' => 'Id',
            'value' => '$data->expense_id',
            'htmlOptions' => array('style' => 'min-width:20px;')
        ),
        array(
            'name' => 'expense_name',
            'header' => 'Expense Name',
            'value' => '$data->expense_name',
            'htmlOptions' => array('style' => 'min-width:150px;')
        ),
        array(
            'name' => 'added_dt',
            'header' => 'Added Date',
            'value' => '$data->added_dt',
            'htmlOptions' => array('style' => 'min-width:100px;')
        ),
        array(
            'name' => 'actv_sw',
            'header' => 'Active',
            'filter' => array('Y' => 'Yes', 'N' => 'No'),
            'value' => '($data->actv_sw=="Y")?("Yes"):("No")',
            'htmlOptions' => array('style' => 'min-width:50px;')
        ),
        array(
            'name' => 'cre8_ts',
            'header' => 'Created Time',
            'value' => '$data->cre8_ts',
            'htmlOptions' => array('style' => 'min-width:100px;')
        ),
        array(
            'name' => 'cre8_by',
            'header' => 'Created By',
            'value' => '$data->user_name',
            'htmlOptions' => array('style' => 'min-width:50px;')
        ),
        array(
            'name' => 'lst_updt_ts',
            'header' => 'Updated Time',
            'value' => '$data->lst_updt_ts',
            'htmlOptions' => array('style' => 'min-width:100px;')
        ),
        array(
            'name' => 'lst_updt_by',
            'header' => 'Updated By',
            'value' => '$data->user_name',
            'htmlOptions' => array('style' => 'min-width:50px;')
        )
    )
        )
);

模型定义如下,

class Expense extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'm_expense';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('expense_name, actv_sw, added_dt, cre8_ts, cre8_by', 'required'),
            array('cre8_by, lst_updt_by', 'numerical', 'integerOnly' => true),
            array('expense_name', 'length', 'max' => 100),
            array('actv_sw', 'length', 'max' => 2),
            array('lst_updt_ts', 'safe'),
            array('expense_id, expense_name, actv_sw, added_dt, cre8_ts, cre8_by, lst_updt_ts, lst_updt_by', 'safe', 'on' => 'search')
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array(
            'cre8By' => array(self::BELONGS_TO, 'MUser', 'cre8_by'),
            'lstUpdtBy' => array(self::BELONGS_TO, 'MUser', 'lst_updt_by'),
            'tExpensesPayments' => array(self::HAS_MANY, 'TExpensesPayment', 'expense_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'expense_id' => 'Expense',
            'expense_name' => 'Expense Name',
            'actv_sw' => 'Active Status',
            'added_dt' => 'Added Date',
            'cre8_ts' => 'Cre8 Ts',
            'cre8_by' => 'Cre8 By',
            'lst_updt_ts' => 'Lst Updt Ts',
            'lst_updt_by' => 'Lst Updt By',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        $criteria = new CDbCriteria;

        /* $criteria->compare('expense_id', $this->expense_id);
          $criteria->compare('expense_name', $this->expense_name, true);
          $criteria->compare('actv_sw', $this->actv_sw, true);
          $criteria->compare('added_dt', $this->added_dt, true);
          $criteria->compare('cre8_ts', $this->cre8_ts, true);
          $criteria->compare('cre8_by', $this->cre8_by);
          $criteria->compare('lst_updt_ts', $this->lst_updt_ts, true);
          $criteria->compare('lst_updt_by', $this->lst_updt_by); */

        return new CActiveDataProvider('Expense', array(
            'criteria' => array(
                'with' => array('cre8By' => array('joinType' => 'LEFT JOIN'))
            )
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Expense the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}

其实我想显示用户名而不是cre8_by& CG64View中的lst_updt_by列,帮我解决这个问题。在此先感谢...............

1 个答案:

答案 0 :(得分:1)

替换 <{1}}来自

'value' => '$data->cre8_by',

然后

将<{1}}替换为

'value' => '$data->cre8By->user_name',

您必须在搜索中添加其他关系:

'value' => '$data->user_name',