Question

I need to print out the result in CActiveDataProvider with CGridView and with pagination The following is my function in model

public function getCompaniesJobsByCompanyId ( $companyId ) 
{
    $criteria = new CDbCriteria(array(
        'with'=>array(
            'jobs'=>array(
                'scopes' => array( 'offline' => array(0), ),
                'vacancies'=>array(
                    'scopes'=>array(
                        'removed'  => array(0),
                        'archived' => array(0),
                    ),
                ),
            ),
        ),
        'condition' => $this->getTableAlias(false) . '.company_id = ' . (int) $companyId,
        )
    );
    $criteria->together = true;
    $dataProvider = new CActiveDataProvider($this, array(
        'criteria'   => $criteria,
        'pagination' => array(
            'pageSize' => 20, //Yii::app()->params['pageSize'],
        ),
    ));
    return $dataProvider;
}

How could be the CGridView to render my data?

By this way I iterate the result

    $dataProvider = Employers::model() -> getCompaniesJobsByCompanyId(2);
    foreach ( $dataProvider->getData() as $data ) {
        echo $data['name'];
        foreach ( $data->jobs as $jobs ) {
            echo ' ----    ' .($jobs->employer_id) . '    ---- ';
            foreach ( $jobs->vacancies as $vacancies ) {
                echo '<br />' . ($vacancies->id) . '<br />';
            }
        }
    }

And My view

$this->widget('zii.widgets.grid.CGridView', array(
     'id'=>'user-grid',
     'dataProvider' => $dataProvider,
     'columns'=>array(
     'title',          // display the 'title' attribute
     'id',  // d
     array(
         'name'=>'job id',
         //'value'=> '$data->jobs[0]["id"]',
         //'value'=> 'jobs.id',
         //'type' => 'raw'
     ),
     array(
         'name'=>'vacancy id',
         //'value'=> '$data->jobs[0]->vacancies[0]->id',
         //'value'=> 'print_r($data->jobs[0])',
         'value'=> '$data->jobs["id"]',
         //'type' => 'raw'
     ),
      array(
          'name'=>'employer name',
          'type'=>'raw', // to encode html string
          'value'=>'$data->name',
      ), 
   ),
));

Any one can help me to print the values in jobs and vacancies relations?

UPDATE I tried adding 'value' => '$data->jobs->id' but get an error Trying to get property of non-object

Update : I tried 'value' => '$data->jobs[0]["id"]' and it display the the result correctly, but if there are only 1 record on the table. When there is more than 1 record on the table, I need to print all result, how to loop on it ?

Was it helpful?

Solution

This line 'value' => '$data->jobs->id' raised an error Trying to get property of non-object because you have been permitted to accessed the property of object instead of array of objects (jobs)

The workaround is you declare a function to do the task on the controller which rendered your gridview

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        ...
        array(            
            'name'=>'newColumn',
            //call the method 'gridDataColumn' from the controller
            'value'=>array($this,'gridDataColumn'), 
            'type'=>'raw'

        )
    ),
));



class MyController extends Controller 
{
     //called on rendering the column for each row 
     protected function gridDataColumn($data,$row)
     {
          $cellResult = "";
          //do your loop here
          //example

          foreach ( $data->children as $child ) {
            $cellResult .=$child->id . "<br/>";
            $cellResult .=$child->name . "<br/>";
         }

         return $cellResult ;    
    }       
   ...  

}

Yii Tutorial

CGridView: Render customized/complex datacolumns

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top