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" => "yiigridSerialColumn"],
// "id",
//"name",
"jid",
"nick",
"remark",
// "rtype",
[
"attribute"=>"group_name",
"value"=>"user2devicesgGp.group_name",
"label"=>"分组名称"
],
"created_at",
["class" => "yiigridActionColumn"],
],
]); ?>声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 移动端web页面input+fixed布局bug总结
- 下一篇: 移动端关闭当前窗口/关闭当前页面
