如何使用 INNER JOIN 在 GridView::widget 中显示值
How to show values in GridView::widget using INNER JOIN
伙计们,我很新,所以请帮助我。
这是我的 SQL:
SELECT tb1.login, tb2.user
来自 tb1
内部连接 tb2 ON tb1.login = tb2.user
我不知道如何在查询中生成它,所以我可以在 GridView 中显示值。
我做到了,但根本不起作用。
$query = TB1::find()->select(['tb1.login', 'tb2.user'])
->innerWith(TB2::tablename(), 'tb1.login = tb2.user');
return new ActiveDataProvider([
'query' => $query
]);
我的网格视图
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'tb1.login',
],
[
'attribute' => 'tb2.user',
}
],
],
]);
在我检查我的 gridview 后说“未设置”,请帮助我!
我很新,对不起[=13=]
处理此用例的“正确”方法是使用关系方法 (https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#relational-data)
因此您应该在 TB1 中定义一个方法“hasXXX”(基于您的关系),然后您可以使用“值”选项在 GridView 中访问它
[
'attribute' => 'packageName', // it has to be defined in the model
'value' => function (Contract $model) {
return $model->package->name;
},
]
Yii2 将处理 SQL 和一切...
或
您可以在 TB1 模型中添加 public 属性 $publicName
和 $userName
并在您的 SQL select(['tb1.login AS loginName', 'tb2.user AS userName'])
中设置别名.但我认为这是快速且肮脏的解决方案。
为了更明确地帮助您:
在tb1模型中添加如下连接函数:
public function getTable2(){
return $this->hasOne(Tb2ModelNameHere::className,['tbl2_id'=>'tbl1_fk_id']);
}
其中'tbl2_id'和'tbl1_fk_id'分别是连接table2和1的字段
在 Gridview 中,您可以简单地调用连接,它会处理查询:
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'login', // presuming login is an attribute of tbl1
'table2.field_name_here',
// or
[
'attribute' => 'table2.field_name_here',
],
],
]);
以上代码假定:
- DataProvider 来自 Table1。函数“getTable2”必须位于当前数据提供者的模型中。换句话说:dataProvider 来自 Table1 并且在 Table1 模型中,我们添加了函数“getTable2”
要调用函数“getTable2”,我们在 gridview 的属性中使用“table2”。 Yii 自动添加“get”并自动将第一个字母大写。因此“table2.user_name”将调用连接函数“getTable2”并从 Table2
中检索 user_name 字段
伙计们,我很新,所以请帮助我。 这是我的 SQL:
SELECT tb1.login, tb2.user 来自 tb1 内部连接 tb2 ON tb1.login = tb2.user
我不知道如何在查询中生成它,所以我可以在 GridView 中显示值。 我做到了,但根本不起作用。
$query = TB1::find()->select(['tb1.login', 'tb2.user'])
->innerWith(TB2::tablename(), 'tb1.login = tb2.user');
return new ActiveDataProvider([
'query' => $query
]);
我的网格视图
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'tb1.login',
],
[
'attribute' => 'tb2.user',
}
],
],
]);
在我检查我的 gridview 后说“未设置”,请帮助我! 我很新,对不起[=13=]
处理此用例的“正确”方法是使用关系方法 (https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#relational-data)
因此您应该在 TB1 中定义一个方法“hasXXX”(基于您的关系),然后您可以使用“值”选项在 GridView 中访问它
[
'attribute' => 'packageName', // it has to be defined in the model
'value' => function (Contract $model) {
return $model->package->name;
},
]
Yii2 将处理 SQL 和一切...
或
您可以在 TB1 模型中添加 public 属性 $publicName
和 $userName
并在您的 SQL select(['tb1.login AS loginName', 'tb2.user AS userName'])
中设置别名.但我认为这是快速且肮脏的解决方案。
为了更明确地帮助您:
在tb1模型中添加如下连接函数:
public function getTable2(){
return $this->hasOne(Tb2ModelNameHere::className,['tbl2_id'=>'tbl1_fk_id']);
}
其中'tbl2_id'和'tbl1_fk_id'分别是连接table2和1的字段
在 Gridview 中,您可以简单地调用连接,它会处理查询:
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'login', // presuming login is an attribute of tbl1
'table2.field_name_here',
// or
[
'attribute' => 'table2.field_name_here',
],
],
]);
以上代码假定:
- DataProvider 来自 Table1。函数“getTable2”必须位于当前数据提供者的模型中。换句话说:dataProvider 来自 Table1 并且在 Table1 模型中,我们添加了函数“getTable2”
要调用函数“getTable2”,我们在 gridview 的属性中使用“table2”。 Yii 自动添加“get”并自动将第一个字母大写。因此“table2.user_name”将调用连接函数“getTable2”并从 Table2
中检索 user_name 字段