yii2中gridview多表关联显示并可查询


模型修改

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'],
    ],
]); ?>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jony0303

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值