Help Relation Of 3 Model

Model 1

============

cargo_loader

============

loader_id

loader_name

loader_price

#cargo_loader has many cargo_details

Model 2

============

cargo_details

============

cargo_id

cargo_total

loader_id

#cargo_detailshas many cargo_items

Model 3

============

cargo_items

============

cargo_id

cargo_length

cargo_width

cargo_height

cargo_actualWeight

/************** My Problem ***********************/

i want to view in the cgridview

[cargo_items->cargo_actualWeight] in my cgrid view, but my cgridview provider from "cargo_loader" model

heres my relation of my model "cargo_loader"


public function relations()

	{

		

            'cd' => array(self::HAS_MANY,'Cargo_details',array('loader_awb'=>'cargo_id'),'through'=>'dim'),

            'ic'=>array(

                        self::HAS_MANY,'cargoitems',array('cargo_id'=>'cargo_id'),'through'=>'cd'

                       ),

                  

              

                    

		);

	}



my cgridview




$this->widget('bootstrap.widgets.TbGridView',array(

	'id'=>'sea-sales-monitoring-grid',

        'type'=>'striped bordered condensed',

	'dataProvider'=>$model->salesReport(),

	'columns'=>array(

                array(

                        'filter'=>true,

                        'name'=>'Act Wt',

                        'value'=>'sum($data->ic->cargo_actualWeight)'

                ),


	),

)); 




iwant to get the sum of cargo_actualWeight, and display in cgridview…

thanks in advance

I think you can use STAT relation for displaying the sum of cargo_actualWeight.

Maybe something like this:




public function relations()

	{

		

            'cd' => array(self::HAS_MANY,'Cargo_details',array('loader_awb'=>'cargo_id'),'through'=>'dim'),

            'ic'=>array(

                        self::HAS_MANY,'cargoitems',array('cargo_id'=>'cargo_id'),'through'=>'cd'

                       ),

            'tw'=>array(

                        self::STAT,'cargoitems',array('cargo_id'=>'cargo_id'),'through'=>'cd',

                               'select'=>'sum(cargo_actualWeight)'

                       ),

		);

	}



http://www.yiiframework.com/doc/guide/1.1/en/database.arr#statistical-query

http://www.yiiframework.com/doc/api/1.1/CStatRelation

Hmm, STAT relation may not support the ‘through’ option … I’m not sure.

But I think the displaying of the sum is quite easy, because you have already established a HAS_MANY relation to the cargo items.

Anyway, if you want to sort or filter by this value, you have to take another way.

http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview/