beginner
beginner

Reputation: 2032

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.

Upvotes: 0

Views: 569

Answers (1)

ThanhPV
ThanhPV

Reputation: 463

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.

Upvotes: 4

Related Questions