Hoang NK
Hoang NK

Reputation: 550

yii2 hasOne relation work wrong

I have 2 tables : users and favorite the following :

users table

favorite

now I establish a relation between them like the following in Favorite model

public function getUser()
{
    return $this->hasOne(User::className(), ['id', 'user_favorited']);
}

in Controller I find a list of user's favorites

public function actionGetList()
{
    $favorite = Favorite::find()->where([
              'user_favoriting' => Yii::$app->user->id
          ])->all();
    foreach ($favorite as $key => $item) {
        # code...
        echo "<pre>"; var_dump($item->user); echo "<br/>"; die('123');
    }
    return $favorite;
}

But when I make a request to this action I get an error

Column not found: 1054 Unknown column '0' in 'where clause'\nThe SQL being executed was: SELECT * FROM `users` WHERE (`0`, `1`) IN ((12, 80))",

Please help me!

Upvotes: 3

Views: 13220

Answers (1)

Touqeer Shafi
Touqeer Shafi

Reputation: 5264

According to documentation you have to use:

public function getUser()
{
    return $this->hasOne(User::className(), ['id' => 'user_favorited']);
}

Upvotes: 4

Related Questions