I am working with Yii2 REST api
and using Authorisation : Bearer
for authentication.
I have a model Event
and only 2 actions Create
and Update
but my Update
action is not working fine and throws Object Class conversion error.
I am using following code for finding Event
model with mixed condition.
public function actionUpdate($id)
{
$params=$_REQUEST;
/*Following line throws error */
$model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id'=> Yii::$app->user->identity]);
if($model !== null){
$model->attributes=$params;
$model->partner_id = Yii::$app->user->id;
$model->updated_date = time();
if ($model->save()) {
$this->setHeader(200);
echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
}
}
}
The error is something like this
Object of class api\modules\v1\models\User could not be converted to string
I cant figure out why it says i have created object of User
class.
The issue is here:
andWhere(['partner_id'=> Yii::$app->user->identity])
You ARE attempting to convert a user object (
Yii::$app->user->identity
) to a string. Instead, you need to use the user's id (Yii::$app->user->identity->id
) which is a string.