I am using Yii2.0 and I have following error when I doing filtering with relationship:
Exception (Database Exception) 'yii\db\Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'userContact.email' in 'where clause' The SQL being executed was: SELECT
tbl_user.* FROMtbl_userLEFT JOINtbl_user_contactONtbl_user.id=tbl_user_contact.user_idWHEREuserContact.
And it is obvious that the table name alias is not given. Following is my code that generate the query above:
Class Files
class User extends ActiveRecord{
public function getUserContacts(){
return $this->hasMany(UserContact::className(), ['user_id' => 'id']);
}
}
class UserContact extends ActiveRecord {
public function getUser(){
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
}
Query
User::find()->joinWith('userContacts', false)
->where(['userContact.email' => $email])
->one();
I follow the instruction given here.
Is there a way to have the alias in the query?