0

如何在CDetailView Yii中列名不同的情况下显示来自外键的数据

表格1

x1      x2
1       sample text 1
2       sample text 2
3       sample text 3

表2

y1      y2          y3 (foreign key x1)
1       text 1      1
2       text 2      1
3       text 3      2

我想显示以下结果

y1      y2          y3
1       text 1      sample text 1
2       text 2      sample text 1
3       text 3      sample text 2

这是模型类中的关系代码

public function relations(){
    return array(
         'Table2' => array(self::BELONGS_TO, 'Table1', array('x1'=>'y3'))
    );
}

这是我的 CDetailView 代码

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
         array(
             'name'=>'Table2.y3',
             'value'=>$model->Table2->x2,
         ),
         'y1',
         'y2'
    ),
)); ?>

我收到以下错误

Property "Table2.x1" is not defined. 
4

1 回答 1

0

第一个问题:关系定义不正确(据我从提供的信息中了解)

模型中的关系Table1

public function relations(){
    return array(
         'Table2' => array(self::HAS_MANY, 'Table2', 'y3')
    );
}

由于 table1 的模型的一个对象将有许多 table2 的对象

模型中的关系Table1

public function relations(){
    return array(
         'Table1' => array(self::BELONGS_TO, 'Table1', 'y3')
    );
}

由于 table2 模型的一个对象属于某个表的对象..

现在在正确定义关系之后,您可以使用'value'=>$data->Table2->x2,从关系中打印值..

于 2014-02-02T15:17:20.570 回答