2017-09-04 14 views
0

Yii2のdropDownListで選択した値($ model->属性)を取得していません。何が間違っている可能性がありますか?おかげYii2のdropDownListの値を取得する方法は?

これは、ビュー上に配置されているコードです:

public function actionTest() 
    { 
     $model = new TestForm(); 

      if ($model->load(Yii::$app->request->post()) && $model->validate()) { 
        $model->insertTest(); 
       return $this->renderAjax('test', ['model' => $model]);  

      } else { 
       return $this->renderAjax('test', ['model' => $model]); 
      } 

    } 

$command = $connection->createCommand('SELECT att1 
              FROM table 
               ORDER By table_id ASC'); 
    $rows = $command->queryAll(); 
    $command->execute(); 

    $listData = ArrayHelper::index($rows, null, 'table_id'); 

はその後、同じビュー上で、私はこれはコントローラである

<div class="row"> 
     <div class="col-lg-9"> 

      <?php Pjax::begin(['enablePushState' => false]); ?> 

      <?php $form = ActiveForm::begin(['id' => 'test-form', 'options' => ['data-pjax' => true] 
      ]); ?> 

       <?= $form->field($model, 'attribute')->dropDownList($listData, ['prompt'=>'List of attributes.']); ?> 


       <div class="form-group"> 
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'test-button']) ?> 
       </div> 

      <?php ActiveForm::end(); ?> 
      <?php Pjax::end(); ?> 

     </div> 
</div> 

$ listDataプロパティを呼び出します

モデルは$ attributeの定義を持ち、insertTest()は$ attributeの値を使って関数を呼び出す関数ですDB上に移動します。あなたは一次元配列を必要とするので、あなたの

+0

あなたは '$ model'をどのように開始するのかを示す必要があります。おそらくコントローラーにあり、POST/GETリクエストからロードされます。 – lubosdz

答えて

2

思考は

ArrayHelper::map($listData, 'table_id', 'table_id');

を使用する必要があります。

そして、データベースへのクエリにActiveRecordを使用する方がよいでしょう。

ArrayHelper Docs

1

あなたがこのコラム

$listData = ArrayHelper::index($rows, 'att1'); 

をマッピングする必要がありますので、あなたはテーブルからのみatt1を選択しているデバッグのためにあなたのactionTestはコメント修正してみてください$モデル - > insertTest(); $ model-> save();を使用します。 値をDBに保存されているならば、あなたは$モデル - > insertTest()関数

public function actionTest() 
    { 
     $model = new TestForm(); 

      if ($model->load(Yii::$app->request->post()) && $model->validate()) { 
       // $model->insertTest(); 
       $model->save(); 
       return $this->renderAjax('test', ['model' => $model]);  

      } else { 
       return $this->renderAjax('test', ['model' => $model]); 
      } 

    } 
+0

あなたの答えをありがとう、dropDownListのリストを生成するより良い方法ですが、まだ選択された値をフィールド($ model、 'attribute') - > dropDownList($ listData、 'prompt' => '属性のリスト。']); ?> –

+0

あなたの問題は何ですか..リストにデータがありませんか? ..あなたは提出するときに価値がない? .. better better please – scaisEdge

+0

dropDownListは、次のような値のリストを表示します。0 \ n attribute1 1 \ n attribute2 2 \ n attribute3 3 \ n attribute4 ...問題は、attribute3を選択すると、サブミット後に変数$属性を取得します。私は間違って何をしていたでしょうか?ありがとう。 –

0

の中身をチェックする必要があり、私はその問題の解決策を見つけました。

このようにすると、フォームは送信された情報を保存できませんでした。

$command = $connection->createCommand('SELECT att1 
               FROM table 
                ORDER By table_id ASC'); 
    $rows = $command->queryAll(); 
    $command->execute(); 

    $listData = ArrayHelper::index($rows, null, 'table_id'); 

しかし、この方法では、フォームは選択された値を取得して変数に入れることができます。

$rows = table::find()->all(); 
$listData = ArrayHelper::map($rows,'table_id','att1'); 
1

あなたはArrayHelper

$listdata = ArrayHelper::map(CategoryModel::find()->orderBy(['table_id' => SORT_ASC])->all(), 'table_id', 'table_text'); 

ノートでマップを使用する必要があります: 'table_textは、' ドロップダウンラベルに表示されますテーブルの属性です。

関連する問題