CJavaScript::jsonEncode and AR relations

Hi all!

I’m saving some records via ajax, and then I return them as json. The problem I’m facing is that no relations are included in the response… it only encodes the parent record attributes… is there a way to also include the relations?

This is the parent record relations…

class Comment extends CActiveRecord {


public function relations() {


        return array(

            'user' => array(self::BELONGS_TO, 'User', 'user_id'),





and this is how I return the record…

$model = new Comment();

$model->user_id = user()->id;



$data['model'] = $model;

echo CJavaScript::jsonEncode($data);

I have also tried whith the $model->refresh() function, and selecting the record again with Commen::model()->with(‘user’)->findByPK… but it doesn’t work either…

Thanks very much in advance!

Hi, there are a couple of extensions that may help you get where you want:




Thanks for you repply! I’m going to check them right now!