Search code examples
phpyiicgridview

Changing column name in yii cgridview based on database value


I am stuck in a situation where I have to change the column header based on database value. e.g If database value is 1, column name should be 'Kick Off', If its 2 then column name should be 'Tip Off'. I used following code but its not working.

array(
    'name' => 'date', 
    'header' => '($data->league_id==1) ? "Kick Off" : "Tip Off"',
    'type' => 'raw',
    'value' => 'strtoupper(date("D M d", strtotime($data->date)))',
),

Solution

  • If you see CDataColumn (default if you use an array to specify the attributes), the header value is a string and will not be evaluated as a PHP expression.

    If you take a look at CDataColumn's renderHeaderCellContent() method, you will notice that it uses the attribute name from the model.

    $this->grid->dataProvider->model->getAttributeLabel($this->name)
    

    So I don't see any problem with placing this in your model, which is where it should have belonged in the first place:

    public function attributeLabels() {
        return array(
            'date' => '($this->league_id == 1) ? "Kick Off" : "Tip Off"',
            ...