Yii - CGridView activerecord关系

时间:2013-08-12 14:16:55

标签: yii yii-relations

我需要使用CGridView和分页在CActiveDataProvider中打印出结果 以下是我在模型中的功能

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;
}

CGridView如何呈现我的数据?

通过这种方式我迭代结果

    $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 />';
            }
        }
    }

我的观点

$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',
      ), 
   ),
));

任何人都可以帮我打印就业和职位空缺关系中的价值观吗?

更新 我尝试添加'value'=&gt; '$ data-&gt; jobs-&gt; id'但是收到错误试图获取非对象的属性

更新: 我试过'value'=&gt; '$ data-&gt; jobs [0] [“id”]'并正确显示结果,但如果表中只有1条记录。当桌面上有超过1条记录时, 我需要打印所有结果,如何循环它?

1 个答案:

答案 0 :(得分:0)

此行'value' => '$data->jobs->id'引发错误尝试获取非对象的属性,因为您被允许访问对象的属性而不是对象数组(jobs

解决方法是声明一个函数来在渲染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