0

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 Updateaction 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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mike Ross
  • 2,942
  • 5
  • 49
  • 101

3 Answers3

2
Yii::$app->user->identity

is object you should use

Yii::$app->user->identity->id

so final line will be:

$model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id'=> Yii::$app->user->identity->id]);
Chetan Sharma
  • 2,539
  • 5
  • 25
  • 41
1

The problem is with your andWhere(), you are trying to assign partner_id an object viz. Yii::$app->user->identity, so this is where your code is breaking. And do not use json_encode when you can use Yii's response format Response::FORMAT_JSON, so your code would be like:

public function actionUpdate($id)
{
    \Yii::$app->response->format = yii\web\Response::FORMAT_JSON; // formatting response in json format
    $params= json_decode(\Yii::$app->request->rawBody, 1);
    /*Following line throws error */
    $model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id'=> Yii::$app->user->identity->id]);

    if($model !== null){

        $model->attributes=$params;
        $model->partner_id = Yii::$app->user->id;
        $model->updated_date = time();

        if ($model->save()) {

            $this->setHeader(200);
            return array('status'=>1,'data'=> $model); // you can simply use $model

        }

    }

}
Sagar Guhe
  • 1,041
  • 1
  • 11
  • 35
  • thank you for your time but that isn't my problem at the moment though. – Mike Ross May 17 '16 at 07:12
  • 1
    I think the problem is in your `andWhere()` condition. You have used `Yii::$app->user->identity` where `identity` is an complete object which has data about logged in user. So it should be like `Yii::$app->user->identity->anyAttributeFromUserTable` – Sagar Guhe May 17 '16 at 07:17
  • i think that was the problem but with the update action i am having problem with `PUT` and `PATCH` request. I dont seem to get any data sent via `PUT` request. – Mike Ross May 17 '16 at 07:22
  • You shoudn't be using `$_REQUEST`, as you `$_REQUEST` only returns `GET`, `POST`, `COOKIE` data. Because you are using `PUT` or `PATCH` requests I assume you are using `JSON` as a request body, so you should use ` \Yii::$app->request->rawBody`. – Sagar Guhe May 17 '16 at 07:29
1

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.

Bryant Makes Programs
  • 1,493
  • 2
  • 17
  • 39