2017-09-04 2 views
0

Yii2의 dropDownList에서 선택한 값 ($ model-> attribute)을 가져 오지 않고 있는데, 무엇이 잘못 될 수 있습니까? 감사합니다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'); 

그런 다음 같은보기에 나는 $되는 listData에 전화

<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> 

이 컨트롤러입니다

모델은 $ attribute의 정의를 가지고 있으며, insertTest()는 $ attribute의 값을 que로 사용하는 함수이다. DB에. 당신이 1 차원 배열을 필요로하기 때문에 당신의

+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 please please – scaisEdge

+0

dropDownList는 다음과 같은 값의 목록을 표시합니다. 0 \ n attribute1 1 \ n attribute2 2 \ n attribute3 3 \ n attribute4 ... 문제는 속성 3을 선택하면 값이없는 것입니다. 변수 $ 속성을 제출 한 후 움 직입니다. 나는 무엇을 잘못 할 수 있었 을까? 감사. –

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 것은'드롭 다운 라벨에 표시됩니다 테이블 속성입니다.

관련 문제