我需要使用 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 条记录时,我需要打印所有结果,如何循环?