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'
...
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
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.
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.
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: