A
A
Antago2014-07-26 16:44:17
JSON
Antago, 2014-07-26 16:44:17

How to return Active Record as json with dependencies in Yii2?

Yii::$app->response->format = Response::FORMAT_JSON;
$model =  Post::find()->with('comments')->all();
return $model;

gives out json of posts without comments, although if you do var_dump($model) they are there.
Judging by yiiframework.ru/forum/viewtopic.php?f=3&t=6997 in the first version of the framework, this could be solved by rewriting json encode. Are there other options now?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Antago, 2014-08-10
@Antago

I found the solution myself - to make a selection asArray()
$model = Post::find()->with('comments')->asArray()->all();
In this case, child objects are also translated into json.

M
Maxim Grechushnikov, 2015-09-02
@maxyc_webber

fields, extrafields?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question