dt250827 2014-09-04 20:50
浏览 33
已采纳

Yii将相关数据导入GridView

My view lets the user see a person's details, including the families they are in. This is done in the DB with a Person table, Family table, and a familyMembership link table between the two.

The relationship in the CActiveRecord model for Person is as such:

'families' => array(self::MANY_MANY, 'Family', 'familymembership(Person, Family)'),

In my person controller, I am wanting to pass a variable into the view that has the related data in a way that TbGridView (CGridView but Bootstrap) will accept (a data provider). The controller calls the person model's getFamilies() function:

public function getFamilies() {
    // returns an array of Family objects related to this Person model
    $familiesArray = $this->getRelated('families');
    // puts this array into a CArrayDataProvider
    $families = new CArrayDataProvider($familiesArray);
    return $families;
}

The return value goes back to the controller, which is then handed through in the renderPartial() call. The view has a TbGridView widget initialisation like this:

$this->widget('bootstrap.widgets.TbGridView', 
            array(
                //the CArrayDataProvider from the model function
                'dataProvider' => $families, 
                'columns' => array(
                    array(
                        'name' => 'Family Name',
                        // Example field from Family model
                        'value' => '$data->familyName' 
                    )
                )
        ));

However, in doing this I am getting the following error: 'Property "Family.id" is not defined. (D:\wamp\www\yii\framework\base\CComponent.php:130)'

The Family model does not have an id property, and I don't understand why the widget is looking for such.

What's going wrong here?

Thanks.

  • 写回答

1条回答 默认 最新

  • dpl22899 2014-09-04 22:50
    关注

    Quoting the doc from Yii Class Reference, you have to provide a keyField:

    Elements in the raw data array may be either objects (e.g. model objects) or associative arrays (e.g. query results of DAO). Make sure to set the keyField property to the name of the field that uniquely identifies a data record or false if you do not have such a field.

    By default keyField will be set to "id", so you need to overwrite it with your Family model primary key :

    <?php
    $familyDataProvider = new CArrayDataProvider($rawData, array(
        'keyField' => 'yourPkNameHere',
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用