显示Yii CGridView中两个表的数据

时间:2014-01-03 08:27:00

标签: php yii

我想显示2个带右连接的表,但我写的代码没有按预期工作。谁能告诉我我做错了什么?

view:admin.php

$this->widget('bootstrap.widgets.TbGridView', array(
    'id' => 'punish-grid',
    'dataProvider' => $model->searchJoin(),
    'type' => 'striped bordered condensed',
    'filter' => $model,
    'columns' => array(
        array(
            'header' => 'No',
                'type'=>'raw',
                'htmlOptions'=>array('style'=>'width: 25px'),
                'value'=>'$this->grid->dataProvider->pagination->currentPage
                       *$this->grid->dataProvider->pagination->pageSize + $row+1',
            ),
            // i want to display p.kode,p.status from table status
            'berlaku_punish',
            'nilai',
            array(
                'class'=>'bootstrap.widgets.TbButtonColumn',
            ),
        ),
));

和我的模型:BasePunish.php

public function relations() {
    return array(
        'idStatus' => array(self::BELONGS_TO, 'Status', 'id_status'),
    );
}

public function searchJoin() {
$criteria = new CDbCriteria;
    $criteria->select = 'p.kode,p.status,t.nilai,t.berlaku_punish';
    $criteria->join= 'RIGHT JOIN status p ON (t.id_status=p.id)';
    $criteria->condition = 't.id_status IS NULL';

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
            'sort'=>array(
                    'defaultOrder'=>'kode ASC',
            ),
        )
    );
}

1 个答案:

答案 0 :(得分:0)

我可能真的不明白你在问什么,但如果没有什么对你有用那么你可以试试这个

array(
'header'=>'Products',
'value'=>array($model,'gridCreateUser'),
),

在此,将尝试在 $ model 作为对象的类中找到函数 gridCreateUser 。在你的情况下,我猜$ model是 BasePunish。的对象

因此,在 BasePunish.php 中创建一个函数 gridCreateUser(),然后您可以返回要在窗口小部件中显示的值。

例如:- 在 BasePunish.php

public function gridCreateUser($data)
{
// you can access the object $data here
// do what ever you want to do
$value='return whatever you want to return';
return $value;
// this $value will be displayed there
}