Search code examples
yiiyii-extensions

How to access relationship data in groupgridview with extraRowColumns


The Database Tables:

project_master (id, project_name)  
task_master (id, task_name, project_id)

Relationship in the TaskMaster Model:

TaskMaster.php

class TaskMaster extends CActiveRecord
{
    /**
     * @return array relational rules.
     */
    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(
            'ProjectsRpl' => array(self::BELONGS_TO, 'Projects', 'project_id'),
        );
    }
}

Following GroupGridView view file:

Task.php

$this->widget('ext.groupgridview.GroupGridView', array(
      'id' => 'Customer-grid',
      'dataProvider' => $modelCustomer->searchCustomer(),
      //'mergeColumns' => 'project_id',
      'extraRowColumns' => array('ProjectsRpl.project_name'),
      'extraRowPos' => 'above',
      'afterAjaxUpdate' => 'function(){}',
      'columns'=>$columns,
));

GroupGridView reference site.

Getting the following errors:

CException: Column or attribute "ProjectsRpl.project_name" not found!


Solution

  • Only one change in Task.php file.

    $this->widget('ext.groupgridview.GroupGridView', array(
          'id' => 'Customer-grid',
          'dataProvider' => $model->search(),
          //'mergeColumns' => 'project_id',
          'extraRowColumns' => array('project_id'),
          'extraRowPos' => 'above',
          'extraRowExpression' => '"<b style=\"color: black\">".$data->ProjectsRpl->project_name."</b>"',
          'afterAjaxUpdate' => 'function(){}',
          'ajaxUrl' => Yii::app()->createUrl('customer/index'),
          'ajaxUpdate' => true,
          'enablePagination' => true,
          "summaryText" => true,
          'enableSorting' => FALSE,
          'columns'=>$columns,
    ));