hasOne(User::className...)}"."/> hasOne(User::className...)}".">
Home > Article > PHP Framework > What should I do if yii2 hasone reports an error?
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.
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('123'); } return $favorite; }
But when I request this operation, 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!
Recommended: "yii tutorial"
Solution:
According to the documentation, you must use:
public function getUser() { return $this->hasOne(User::className(), ['id' => 'user_favorited']); }
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!