0

A table Product contains has_many relationship with a table **Slab** that further contains has_many relationship with a table Rate.

A relational query is wrapped inside CActiveDataProvider that joins three tables across certain parameters and return products in descending order of their respective rates.

I want to show the results in tabular form through CGridView.

Trying to access only certain columns through following syntax:

$this->widget('zii.widgets.grid.CGridView', array(
                'dataProvider'=>$dataProvider,
                'columns'=>array(
                    'name','slabs.id','slabs.rates.rate'
                )
            ));

Unfortunately I can't access slabs.id because when I dump dataProvider object I see that it is annexed to Product object through an Array whose index[0] contains the Slab object, then under Slab object index[0] has the rates.rate object.

It is probably occurring because of the has_many relationship between tables but my query would always return one Slab and one Rate object.

How may I access and show them?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • check this link http://www.yiiframework.com/wiki/385/displaying-sorting-and-filtering-hasmany-manymany-relations-in-cgridview/ – Manisha Patel Sep 06 '14 at 05:47

1 Answers1

0

I guess there are two possible workarounds.

1. Change or add a relationship so you can access a slab and a rate with a has_one relationship.

2. You can add a column to the gridview with a custom name and value like this:

'columns'=>array(
    'name',
    array(
        'name' => 'Slab Id',
        'value' => '$data->slabs[0]->id',
    ),
    array(
        'name' => 'Rate',
        'value' => '$data->slabs[0]->rates[0]->rate',
    )
)

Where value is a string representing an expression to be evaluated. $data is the main model, in this case Product.

Alfred_P
  • 792
  • 4
  • 8