如何在cgridview(yii框架)中显示两个模型?
我是yii框架的新手。我有两个模型(如何在cgridview(yii框架)中显示两个模型?,yii,cgridview,Yii,Cgridview,我是yii框架的新手。我有两个模型(表有许多周期数据),我想在表索引视图的CGridView中显示模型周期数据,但无法实现 表格列 The followings are the available columns in table 'nkr_tables': @property integer $id @property string $title @property string $periode The followings are the available columns in tab
表
有许多周期数据
),我想在表索引视图的CGridView
中显示模型周期数据
,但无法实现
表格列
The followings are the available columns in table 'nkr_tables':
@property integer $id
@property string $title
@property string $periode
The followings are the available columns in table 'nkr_periode_datas':
@property integer $id
@property integer $table_id
@property string $year
表格模型关系
'periodeData' => array(self::HAS_MANY, 'PeriodeDatas', 'table_id')
'table' => array(self::BELONGS_TO, 'Tables', 'table_id')
周期数据列
The followings are the available columns in table 'nkr_tables':
@property integer $id
@property string $title
@property string $periode
The followings are the available columns in table 'nkr_periode_datas':
@property integer $id
@property integer $table_id
@property string $year
周期数据模型关系
'periodeData' => array(self::HAS_MANY, 'PeriodeDatas', 'table_id')
'table' => array(self::BELONGS_TO, 'Tables', 'table_id')
表格控制器
public function actionIndex()
{
$model = new Tables('search');
$model->unsetAttributes();
if (isset($_GET['Tables']))
$model->attributes = $_GET['Tables'];
$this->render('index', array('model' => $model));
}
表格视图
$this->widget('zii.widgets.grid.CGridView',
array(
'id'=>'tables-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns' => array(
array(
'name' => 'title',
'header' => 'Data Title'
),
'periode',
array(
'name' => 'periodeData.year',
'header' => 'Tahun'
)
),
'selectionChanged'=>'function(id) { location.href = "'.$this->createUrl('tables/view').'/"+$.fn.yiiGridView.getSelection(id);}'
)
);
结果:
如何在表视图的CGridView
中显示模型periodidatas
中的year
*对不起,英语不好
更新:
我尝试添加
'value'=>'$data->periodidata->year'
,但在尝试获取非对象的属性时出现了一个错误
更新:
我尝试了'value'=>'$data->periodData[0]->year'
,它正确地显示了年份,但是如果表上只有一条记录。当表上有多条记录时,它会产生一个错误未定义的偏移量:0
然后我尝试了'value'=>'$data->periodData[$data->id]->year'
并给我错误未定义的索引:1
而不是创建periodData的模型,并在视图中使用表的关系来访问记录
因为你正在访问数组(),而不是一个字符串,它有很多关系
您可能会阅读此线程我在foreachjust Var_dump($model->PeriodData)中访问$model->PeriodData->year
时,试图获取非对象的属性时遇到了一个错误,请查看它是数组还是对象。如果您得到该错误,则表示$model->periodData是一个数组(),而不是一个对象print\r($model->periodData)
返回一个空数组。它应该是2013
$model->periodData将返回一个perioddatas对象数组(如果存在),否则它将返回一个空数组。@Ninad您可能希望该循环看起来像foreach($model->periodData as$periodData)$periodDataYear[]=$periodData->year代码>因为现在,每个循环将用下一个PeriodDatas对象替换数组。