Home > PHP Framework > YII > body text

What should I do if yii2 hasone reports an error?

藏色散人
Release: 2020-07-20 10:46:57
Original
2019 people have browsed it

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
Copy after login

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

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

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;
}
Copy after login

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))",
Copy after login

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;]);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template