yii2多表

5cnsuln7  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(350)

我有两张table strategy 以及 risk_colors
.
我用gii生成了模型和积垢,并对其进行了一点修改以获得 risk_value 我的gridview小部件中的列。
这是我的 Strategy.php ```

smdnsysy

smdnsysy1#

在你的 StrategySearch.php ```
public function search($params)
{
$query = Strategy::find()->joinWith(['riskColors']);

 // add conditions that should always apply here

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

 $dataProvider->sort->attributes['risk_colors_id'] = [
     'asc' => ['risk_colors.risk_value' => SORT_ASC],
     'desc' => ['risk_colors.risk_value' => SORT_DESC],
 ];

 $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,
     'strategy_current_money' => $this->strategy_current_money,
     // 'risk_colors_id' => $this->risk_colors_id,
     'risk_colors.risk_value' => $this->risk_colors_id
 ]);

 $query->andFilterWhere(['like', 'strategy_title', $this->strategy_title])
       ->andFilterWhere(['like', 'strategy_description', $this->strategy_description]);

 return $dataProvider;

}

参考gridview排序

相关问题