如何在yii2搜索模型中使用joinWith关系

如何在yii2搜索模型中使用joinWith关系,yii2,Yii2,我有两个模型和相关数据fbl_联盟和fbl_国家表。fbl_联赛有一个列country_id,它与fbl_country相关,现在在yii2 gridView中,我可以做一些类似[ '属性'=>'国家/地区id', '值'=>'国家/地区名称', ],它给出了国家的名称而不是国家id,然后我还想启用按国家名称而不是国家id进行搜索,但我得到以下错误 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'country.name' in '

我有两个模型和相关数据fbl_联盟和fbl_国家表。fbl_联赛有一个列country_id,它与fbl_country相关,现在在yii2 gridView中,我可以做一些类似
[
'属性'=>'国家/地区id',
'值'=>'国家/地区名称',
],
它给出了国家的名称而不是国家id,然后我还想启用按国家名称而不是国家id进行搜索,但我得到以下错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'country.name' in 'where clause'
The SQL being executed was: SELECT COUNT(*) FROM `fbl_leagues` LEFT JOIN `fbl_country` ON `fbl_leagues`.`country_id` = `fbl_country`.`id` WHERE `country`.`name` LIKE '%s%'
下面是我的联盟搜索模型

class LeagueSearch extends League
{
/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        [['id', 'created_at', 'updated_at'], 'integer'],
        [['name','country_id'], 'safe'],
    ];
}

/**
 * {@inheritdoc}
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = League::find();


    $query->joinWith('country');
    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ]);

    $query->andFilterWhere(['like', 'name', $this->name])
          ->andFilterWhere(['like', 'country.name',$this->country_id]);

    return $dataProvider;
}
}
还有我的联盟模式

 {
    return [
        [['country_id', 'created_at', 'updated_at'], 'integer'],
        [['name','country_id'], 'required'],
        [['name'], 'string', 'max' => 100],
        [['country_id'], 'exist', 'skipOnError' => true, 'targetClass' => Country::className(), 'targetAttribute' => ['country_id' => 'id']],
    ];
}

/**
 * {@inheritdoc}
 */
public function attributeLabels()
{
    return [
        'id' => Yii::t('app', 'ID'),
        'country_id' => Yii::t('app', 'Country Name'),
        'name' => Yii::t('app', 'Name'),
        'created_at' => Yii::t('app', 'Created At'),
        'updated_at' => Yii::t('app', 'Updated At'),
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getCountry()
{
    return $this->hasOne(Country::className(), ['id' => 'country_id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getPerfectSelections()
{
    return $this->hasMany(PerfectSelection::className(), ['league_id' => 'id']);
}

现在我注意到当我注释掉
$query->joinWith('country')
那么就不会有错误,但是搜索没有按预期工作

嗯,我很傻,我想我后来发现了问题,表名是fbl\u country,所以我想写
fbl\u country.name
但是在我的搜索模型中写了
country.name
,不过这是漫长的一天,谢谢大家