Yii将相关数据导入GridView

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.

我的视图让用户可以看到一个人的详细信息,包括他们所在的家庭。这是在数据库中完成的。 一个Person表,Family表和两者之间的familyMembership链接表。 p>

Person的CActiveRecord模型中的关系是这样的: p>

  'families'=> 数组(self :: MANY_MANY,'Family','familymembership(Person,Family)'),
  code>  pre> 
 
 

在我的人物控制器中,我想传递一个变量 进入具有TbGridView(CGridView但Bootstrap)将接受的方式(数据提供者)的相关数据的视图。 控制器调用person模型的getFamilies()函数: p>

  public function getFamilies(){
 //返回与此Person模型相关的Family对象数组
 $ familiesArray  = $ this-> getRelated('families'); 
 //将此数组放入CArrayDataProvider 
 $ families = new CArrayDataProvider($ familiesArray); 
返回$ families; 
} 
  code>   pre> 
 
 

返回值返回到控制器,然后在renderPartial()调用中传递。 该视图有一个TbGridView小部件初始化,如下所示: p>

  $ this->小部件('bootstrap.widgets.TbGridView',
 array(
 // // CArrayDataProvider来自 模型函数
'dataProvider'=> $ families,
'columns'=> array(
 array(
'name'=>'Family Name',
 //来自Family model的示例字段 
'value'=>'$ data-> familyName'
)
)
)); 
  code>  pre> 
 
 

然而,在这样做我 我收到以下错误: 'Property“Family.id”未定义。 (D:\ wamp \ www \ yii \ framework \ base \ CComponent.php:130)' p>

Family模型没有id属性,我不明白为什么 小部件正在寻找这样的。 p>

这里出了什么问题? p>

谢谢。 p> div>

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',
));