模型修改
public function getUser2devicesgGp(){
return $this->hasOne(User2devicesgGp::className(), ['group_id'=>'group_id']);
}// hasOne要求返回两个参数 第一个参数是关联表的类名 第二个参数是两张表的关联关系 public $group_name;
搜索模型修改
public $group_name; //定义传值变量
public function rules()
{
return [
[[ 'rtype'], 'integer'],
[[ 'jid', 'nick', 'remark','group_name'], 'safe'],
];
}
$query = User2devices::find()->where(['name'=>Yii::$app->user->identity->username]);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
$result = User2devicesgGp::find()->where(["group_name"=>$this->group_name,'name'=>Yii::$app->user->identity->username])->one();
// var_dump($this->group_name);
// var_dump($result->group_id);值的关系转换
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,
'rtype' => $this->rtype,
'group_id' => $result->group_id,
]);
$query->andFilterWhere(['like', 'jid', $this->jid])
->andFilterWhere(['like', 'nick', $this->nick])
->andFilterWhere(['like', 'remark', $this->remark])
->andFilterWhere(['like', 'group_id', $result->group_id]);
return $dataProvider;
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
//'name',
'jid',
'nick',
'remark',
// 'rtype',
[
'attribute'=>'group_name',
'value'=>'user2devicesgGp.group_name',
'label'=>'分组名称'
],
'created_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>