1

I have next variables:

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

Now in $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'
    )
    ...

How i can display my products in CGridView? Thx.

3 Answers 3

1

I Suppose you are using CarrayDataProvider. So in your controller

$dataProvider = new CArrayDataProvider($prod);

Here $product could be any array you want to display in CgridView. Now In you view write this.

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

As in CarrayDataprovider array is obtained so we cant use relations in it. Thats why u have to write 'ProductTypeModel::model()->findByPk($data->product_id)->key'
Here you can display anything attribute of ProductTypeModel so u can replace above mentioned key with your desired attribute

0

Try this ... it automatic convert to array data provider.

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

Thanks all. By means of answers "naveen goyal" and "jailed abroad" i did like this:

$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"])',
        ),

)));

Nice work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.