I am still new to Yii and wondering how to return JSON from the $models = MyModel::model()->findAll();
.
Say for example MyModel
has a relation for MyChildModels
in a ONE:MANY fashion.
Straight from the Rest example on the Yii site I have:
foreach ($models as $model) {
$rows[] = $model->attributes;
}
$this->_sendResponse(200, CJSON::encode($rows), 'application/json');
I get all of the model's attributes but NOT the joined relation attributes.
Similarly, I can change the $rows
line to be:
$rows[] = $model->myChildModels;
...and I get all of the myChildModels
attributes for each model, but not any attributes (as I would expect).
But what I want is the full suite - the Model
attributes PLUS all of the myChildModels
and their attributes.
How do I accomplish this?
I do the same thing with Yii. Here is how I do it.
I don't normally sent a JSON header, but you can if you want.
for related models try this.