quickbrownfox quickbrownfox - 4 months ago 40
JSON Question

YII2 json_encode returns empty

Why does this returns empty

{}
?

$model= \common\models\rps\RpsChecklist::findOne($id);
return json_encode($model);


I tried
return json_encode($model->id);
it returns just the exact ID.

Answer

findOne() will return an active record object. In your case is RpsChecklist model.

If you want to use json_encode() function, object must be array. So my solution is:

$model=  \common\models\rps\RpsChecklist::find()->where(['id' => $id])->asArray()->one();
return json_encode($model);

Goodluck and have fun.

Comments