JSON encode of relational query

Hi there,

I started to work with Yii recently and still have a lot to learn. One problem I came across:

I use relational query to pull data like this:




$models=Parent::model()->with('child')->findAll($criteria);



then I feed the data to jqGrid with (I skipped steps here where I fill $page, $total & $records)




$data = array('total' => $total, 'page' => $page, 'records' => $records, 'rows' => $models);

$json_data = new CJSON;

echo $json_data->encode($data);



Unfortunately, json_encode uses only Parent records and does not attach joined Child

I came up with the following solution:




$parents=array();

foreach($models as $model) {

$parents[]=array_merge($model['attributes'],$model['child']['attributes']);

}



and it works

But I don’t like because I’m not using Yii power.

Is there a better (Yii) way to do that?

Thanks,

OR

CHtml::encodeArray package does a very nice job of helping with that see

http://www.yiiframework.com/doc/api/CHtml#encodeArray-detail

nz