Php 如何将数组显示到CGridView(yii框架)

Php 如何将数组显示到CGridView(yii框架),php,yii,Php,Yii,我有下一个变量: $type_model = ProductTypeModel::model()->findByPk($id); $prod = $type_model->product; 现在是$prod: array ( 0 => ProductModel#1 ( [CActiveRecord:_new] => false [CActiveRecord:_attributes] => array

我有下一个变量:

$type_model = ProductTypeModel::model()->findByPk($id);
$prod = $type_model->product;
现在是$prod:

array
(
    0 => ProductModel#1
    (
        [CActiveRecord:_new] => false
        [CActiveRecord:_attributes] => array
        (
            'product_id' => '6'
            'product_type_id' => '5'
        )
        ...
    )
    1 => ProductModel#2
    (
            'product_id' => '8'
            'product_type_id' => '5'
    )
    ...
如何在CGridView中显示我的产品? Thx.

试试这个。。。 它可以自动转换为数组数据提供程序

$dataProvider=new CArrayDataProvider($type_model->product);

我想您正在使用CarrayDataProvider。所以在你的控制器里

$dataProvider = new CArrayDataProvider($prod);
此处$product可以是您希望在CgridView中显示的任何数组。现在 在你看来,写这个

$gridColumns = array(
    array(
        'header' => 'First Name',
        'value' => 'ProductTypeModel::model()->findByPk($data->product_id)->key',
        'htmlOptions' => array('style' => 'text-align:center;')
    ),


$this->widget('zii.widgets.grid.CGridView',array('dataProvider' => $dataProvider,));
正如在
CarrayDataprovider中一样,数组是获得的,因此我们不能在其中使用关系。这就是为什么您必须编写
'ProductTypeModel::model()->findByPk($data->product\u id)->key'


在这里,您可以显示
ProductTypeModel
的任何属性,这样您就可以用所需的属性替换上面提到的
,谢谢大家。通过回答“naveen goyal”和“监禁国外”,我做到了:

$dataProvider=new CArrayDataProvider($type_model->product);
$dataProvider->keyField = 'product_id';
$this->widget('bootstrap.widgets.TbGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
        array(
            'header' => 'Title',
            'value' => 'CHtml::encode($data["product_title"])',
        ),

)));
干得好