我是新来的。
我已经阅读了很多关于加入表格的答案,但似乎无法弄明白。
我想加入2个表 我有很多列的sap_items_slo :(我需要PropertyCode和PropertyName):
id | name | etc.. | PropertyCode | PropertyName
-----------------------------------------------------
5 |Product1 | etc.. | 1 | Fill
75 |Product2 | etc.. | 2 | RTG
55 |Product3 | etc.. | 2 | Implant
和其他带有列的表propery_hr:
PropertyCode | PropertyName
-----------------------------
1 | Fill2
2 | RTG2
3 | Implant2
他们看起来像这样。我想像这样使用它,所以它会为一些用户翻译propertyNames。这些表来自不同的数据库。
我可以调用第一个数据库并正确输出,但是
userCountry = Yii::$app->user->identity->country;
if($userCountry == 'SomeCoutnry') {
*join tables(display ProperyName for each sap_items_slo PropertyCode from property_hr)*
}
这是我的代码 SapItemsSearch.php
public function search($params)
{
$query = SapItems::find()->where(['deleted'=>'no', 'sap_items_slo.country'=>Yii::$app->params['work_country']]);
$query->andWhere("ManufacturerCode !=86 and ManufacturerCode !=84 and ManufacturerCode !=83 and ManufacturerCode !=48");
$query->with(['sapPrice','image']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 1000
]
]);
$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;
}
SapItemsController
public function actionIndex()
{
$searchModel = new SapItemsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$js = "$('#imagemodal').on('shown.bs.modal', function (event) {
var button = $(event.relatedTarget);
var src = button.attr('rel');
var modal = $(this)
modal.find('.modal-body').html(\"<div style='text-align:center;'><img src='https://www.dental-medical.rs/upload/product/\"+src+\"' /></div>\");
})";
$this->getView()->registerJs($js);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
我在gridColumns的index.php中显示它
$gridColumns = [
[
'attribute' => 'PropertyCode',
'label' => 'Property',
'value' => 'PropertyName',
'filter' => ArrayHelper::map(SapItems::find()->select('PropertyCode,PropertyName')->where('PropertyName<>""')->andWhere(['country'=>Yii::$app->params['work_country']])->distinct()->orderBy('PropertyName')->all(),'PropertyCode','PropertyName'),
'filterType'=>GridView::FILTER_SELECT2,
'filterWidgetOptions'=>[
'pluginOptions'=>['allowClear'=>true],
],
'filterInputOptions'=>['placeholder'=>'-- Property --'],
],
//And then echo gridView
echo GridView::widget([
'rowOptions'=> function ($model, $key, $index, $grid) {
if($model->OnHand > 0)
return ['class' => 'success'];
else
return ['class' => 'danger'];
},
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'pjax' => false,
'layout' => '<div style="margin-bottom:10px;"><h2 style="font-size:25px;" class="panel-title pull-left"><i class="glyphicon glyphicon-user"></i> '.$this->title.'</h2><div class="pull-right">{toolbar}</div><div class="clearfix"></div></div>{items}<div class="pull-left" style="margin-top:-20px;">{pager}</div><div class="pull-right">{summary}</div><div class="clearfix"></div>',
我希望这是足够的信息,我在Yii2中得到了这个项目,我不知道该怎么做。我尝试过多种方式,但没有运气。我感谢任何帮助!如果您希望我发布任何其他信息,请填写免费询问。
答案 0 :(得分:1)
要在gridview中使用过滤器连接两个表格,请使用这些sted
在您的SapItems模型中,您需要一个关系的公共函数
我假设propery_hr表有一个名为Property
的模型类public function getProperty()
{
return $this->hasOne(Property::className(),
['PropertyCode' => 'PropertyCode']);
}
然后你可以为PropertyName构建一个getter
public function getPropertyName() {
return $this->property->PropertyName;
}
然后在您的gridview中,您可以使用PropertyName
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summaryOptions' => ['class' =>'dfenx_pagination_summary',],
'pager' => ['options' => ['class'=> 'pagination pull-right']],
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.....
'PropertyName',
.....
对于搜索和过滤,您必须在seachModel中添加一个用于过滤器的公共变量..
public $PropertyName;
为搜索定义此var安全
public function rules()
{
return [
...
[[ 'PropertyName', ], 'safe'],
];
}
在SapItemsSearch中,你必须添加一个与你有关系的联接
if (!($this->load($params) && $this->validate())) {
$query->joinWith(['Property']);
return $dataProvider;
}
此时您可以使用
检索modelSearch中的数据->andFilterWhere(['like','PropertyName', $this->PropertyName])
您可以在此文档http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/
中找到有用的教程来解决问题