2016-03-29 15 views
2

ここに新しいです。Yii2のテーブルの結合

私はテーブルを結合することに関する多くの答えを読んだことがありますが、それを理解できないようです。 (私は必要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 

彼らは

は、私は2つのテーブル 私は列の多くのsap_items_sloているに参加したいですこのように見える。私はこのように使用したいので、一部のユーザーのために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, 
    ]); 
} 

です。 php in gridColumns

 $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でこのプロジェクトを与えられていますが、これを行う方法がわかりません。私は複数の方法で試したが運がない。私は助けていただきありがとうございます!あなたが私に他の情報を投稿してもらいたい場合は、お気軽にお問い合わせください。あなたは関係

の公開機能を必要とする2のテーブルを結合するための

答えて

1

ADNごSapItemsにあなたがこれらのSTED

を必要とするフィルタとのGridViewに使うモデルは、私がpropery_hrテーブルを想定プロパティ

という名前のモデルクラスを持っています
public function getProperty() 
{ 
    return $this->hasOne(Property::className(), 
    ['PropertyCode' => 'PropertyCode']); 
} 

次にあなたがPropertyName意味

public function getPropertyName() { 
    return $this->property->PropertyName; 
} 
のgetterを構築することができます

は、あなたのGridViewにあなたがあなたのseachModelでフィルタのプロパティpublic varを追加する必要があり、検索およびフィルタのPropertyName意味

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'summaryOptions' => ['class' =>'dfenx_pagination_summary',], 
    'pager' => ['options' => ['class'=> 'pagination pull-right']], 
    'filterModel' => $searchModel, 
    'columns' => [ 
     ['class' => 'yii\grid\SerialColumn'], 
     ..... 
     'PropertyName', 
     .....  

を使用することができます。..

public $PropertyName; 

そして、あなたはあなたが

であなたmodelSearchでデータを取得することができ、この時点であなたの関係

 if (!($this->load($params) && $this->validate())) { 
     $query->joinWith(['Property']); 

     return $dataProvider; 
    } 

joinwithを追加する必要がありますSapItemsSearchで検索

public function rules() 
{ 
    return [ 
     ... 
     [[ 'PropertyName', ], 'safe'], 

    ]; 
} 

のために、これは安全なVAR定義

->andFilterWhere(['like','PropertyName', $this->PropertyName]) 

この文書のyuorの問題に関する役に立つチュートリアルを見つけることができますhttp://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

+0

'PropertyName'のゲッターを作成する代わりに、Yiiのドット関係を使用して、' property.PropertyName'をcolumns配列。それは検索のいくつかの調整が必要ですが、頻繁にこれを行う必要がある場合は、余分なコードを書くことからあなたを保存します。 – spencer4of6

+0

病気は今日これをチェックして、それがどのようになっているかを教えてください。どうもありがとうございました! :) –