quickbrownfox quickbrownfox - 1 year ago 398
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download