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

问题描述 投票:0回答:1

我有两个具有相关数据fbl_leagues和fbl_country表的模型。 fbl_leagues有一个与fbl_country相关的country_id列,现在在yii2 gridView中,我可以执行类似[ 'attribute' => 'country_id', 'value' => 'country.name', ],的操作,该操作给出了国家名称而不是国家ID,那么我还想启用按国家名称搜索而不是country_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%'

下面是我的LeagueSearch模型

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');时将没有错误,但搜索无法按预期进行

yii2
1个回答
0
投票

嗯,愚蠢的我,我想我后来想出了表名是fbl_country的问题,所以我想写fbl_country.name但写了country.name,尽管这是漫长的一天,谢谢大家

© www.soinside.com 2019 - 2024. All rights reserved.