0

我需要使用 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' => '$data->jobs->id' 但出现错误 Trying to get property of non-object

更新:我尝试了 'value' => '$data->jobs[0]["id"]' 并正确显示结果,但如果表上只有 1 条记录。当表上有超过 1 条记录时,我需要打印所有结果,如何循环?

4

1 回答 1

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 教程

CGridView:呈现自定义/复杂数据列

于 2013-08-13T03:52:40.693 回答