2016-07-15 93 views
0

为什么这会返回空 {}YII2 json_encode返回空

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

我试过return json_encode($model->id);它只返回确切的ID。

回答

4

findOne()将返回一个活动记录对象。在你的情况是RpsChecklist模型。如果要使用json_encode()函数,则对象必须是数组。 所以我的解决方案是:

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

Goodluck和玩得开心。