hasOne(User::className...)}"."/> hasOne(User::className...)}".">

Home  >  Article  >  PHP Framework  >  What should I do if yii2 hasone reports an error?

What should I do if yii2 hasone reports an error?

藏色散人
藏色散人Original
2020-07-20 10:46:571997browse

yii2 The solution to hasone error: first open the corresponding file according to the documentation; then modify the statement to "function getUser(){return $this->hasOne(User::className...)}" that is Can.

What should I do if yii2 hasone reports an error?

yii2 hasOne relationship working error

Specific problem:

I have 2 tables: Users and Favorites:

users table
favorite

Now, I have a relationship in the "Favorites" model as shown below

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

In the controller, I found List of user favorites

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

But when I request this operation, I get an error

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

Please help me!

Recommended: "yii tutorial"

Solution:

According to the documentation, you must use:

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

The above is the detailed content of What should I do if yii2 hasone reports an error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn