Restful JSON

The question is: How do I get JSON out of Yii?

In the case of a single model, that’s easy. We can just use CJSON::encode().

But what if we have, for example, $message (a Message model queried with ->find()) and $attachments (an array of Attachment models queried with ->findAll()). If they were just arrays, we’d say:

$message['attachments'] = $attachments;

echo json_encode($message);

but they aren’t arrays. What’s the Yii CActiveRecord CEquivelent?


Here’s one answer. But it seems overly complex don’t you think?