yii2 hasone報錯的解決辦法:首先根據documentation打開相應的文件;然后修改語句為“function getUser(){return $this->hasOne(User::className…)}”即可。
yii2 hasOne關系工作錯誤
具體問題:
我有2個表:用戶和收藏者:
users?table favorite
現在,我在“收藏夾”模型中建立了如下所示的關系
public?function?getUser() { ????return?$this->hasOne(User::className(),?['id',?'user_favorited']); }
在控制器中,我找到了用戶收藏夾的列表
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; }
但是當我請求此操作時,我得到一個錯誤
Column?not?found:?1054?Unknown?column?'0'?in?'where?clause'nThe?SQL?being?executed?was:?SELECT?*?FROM?`users`?WHERE?(`0`,?`1`)?IN?((12,?80))",
請幫我!
推薦:《yii教程》
解決方法:
根據documentation,您必須使用:
public?function?getUser() { ????return?$this->hasOne(User::className(),?['id'?=>?'user_favorited']); }
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END