How show model HAS_MANY relation with CGridView

Hi, I’m newbie in Yii and I need your help. (Sorry for my bad englsih)

I have 2 tables:


//service table's fileds

id_sevice, phone_nubmer


//service_checks table's fields

id_check, id_service, attr, val




In Service model

	

	public function relations() {

		return array ('serviceChecks' => array (self::HAS_MANY, 'ServiceCheck', 'id_service' ) );

	}


In ServiceCheck model


	public function relations()

	{

		return array(

			'service' => array(self::BELONGS_TO, 'Service', 'id_service'),

		);

	}

Every service may exist one or more serviceCheckswith different attr.

I must show this services using CGridView with additional columns.

For example:


in service table:

id_service phone_number

1          1234567

2          4567894


in service_checks table

id_check id_service attr              val

1        1          'password'        'somepass'

2        1          'someotherattr'   'sometext'

...

And in gridview I add columns with this way:


array (

	'id_service', 

	'phone_number', 

	array (

	   	'name' => 'serviceChecks.val', 

	    	'value' => '$data->serviceChecks[0]->val'

	),

....

)

here I can only show service checks with index(0). But check may be grow.

I read this topic: http://www.yiiframework.com/forum/index.php?/topic/14590-solved-how-filter-cgridview-with-join-table-and-some-condition-and-paging/page__st__20

But it’s not solution for me.

Nobody can’t help me?

what if you try reading the "child" table instead of the "parent" table? I mean, instead of creating a model for service, create a model for service_checks and use the view like




array (

        'id_service', 

        array('name'=>'service.phone_number', 'value'=>'$data->service->phone_number'),

        'val'

)



Hope it helps

Regards

thanks for your idea, but this is not solution.

I need list of services (in grid view) and near each of this services his service_check->val (where service_check.attr=‘password’ for example)

Kamran, I’m close to trying something similar, though not exactly the same. I’ll tell you my approach / ideas which haven’t been tested yet but might help.

array (

            'name' => 'serviceChecks.val', 


            'value' => 'YourModel::model()->mymethod($data->id_service)',


            'type' => 'raw',


    ),

The idea is that the …mymethod() would take the current id_service, loop through the related service_checks records, and build a chunk of HTML that gets returned as a string. The ‘type’=>‘raw’ allows the HTML. You might be able to put the method in the controller as an alternative. Again, I haven’t tested my approach, so it might not work exactly as described.

What you really need is the ability to nest cgridviews. Setting ‘value’=>‘renderPartial(‘nested_cg’,$data->id_service)’ would be a nice solution where the ‘nested_cg’ is a view that contains a CGridView based on a …“find” using id_service as the search criteria.

Please update the thread if you find a solution.

Kamran, I don’t know if you’re still working on this, but just a quick update. I tried out my approach using a method in the controller to set ‘value’. It worked perfectly.

Basically, the static function looked like this:


    public static function rawTest2($plans) {

        $tmp = '<div class="raw2">';

        foreach ($plans as $plan) {

            $tmp = $tmp.$plan->shares.'<br/>';

        }

        $tmp = $tmp.'</div>';

        return $tmp;

    }

And the CGridView (portion of code) looked like this:


  array(

    'name' => 'Current_Value',

    'value' => 'PlanController::rawTest2($data->planDetails)',

    'type' => 'raw',

  ),

NOTE: planDetails is a relation. It references the records in the joined table that correspond to the current row.

For people reading this post much later. Have a look at this wiki for nested CGridViews:

Wiki