在DropDownList中获取相同的值yii2

时间:2016-10-01 11:55:27

标签: select drop-down-menu yii2

我有一个DropDownList重复三次,但是我使用了3个模型和3个选择ID来产生差异,但是我获得了与其他模型重复的最后一个相同的值。 模型$modelDevisReduction1是表Reduction(type,taux)的模型,因此对于每个选择我都有一个' taux'。 这是我的代码:

 /* Reduction 1 */
<?= $form->field($modelDevisReduction1, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-1'] )->label(false); ?>

<?=  $form->field($modelDevisReduction1,'taux')->textInput(['maxlength' => true,'id'=>'DevisReductionTaux-1'])->label(false); ?>

/* Reduction 2 */
<?= $form->field($modelDevisReduction2, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-2'] )->label(false); ?>

<?=  $form->field($modelDevisReduction2,'taux')->textInput(['maxlength' => true,'id'=>'DevisReductionTaux-2'])->label(false); ?>

/* Reduction 3 */
<?= $form->field($modelDevisReduction3, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-3'] )->label(false); ?>

<?=  $form->field($modelDevisReduction3,'taux')->textInput(['maxlength' => true,'id'=>'DevisReductionTaux-3'])->label(false); ?>

这是控制器actionCreate代码:

public function actionCreate()
{

    $model = new Devis();
    $modelDevisReduction1 = new DevisReduction();
    $modelDevisReduction2 = new DevisReduction();
    $modelDevisReduction3 = new DevisReduction();

    if ($model->load(Yii::$app->request->post()) &&  
    $modelDevisReduction2->load(Yii::$app->request->post()) && 
    $modelDevisReduction1->load(Yii::$app->request->post()) && 
    $modelDevisReduction3->load(Yii::$app->request->post())) {

        echo "Reference => ".$model->reference;
        echo "<br/>";
        echo "dateCreation => ".$model->dateCreation;
        echo "<br/>";
        echo "contact => ".$model->contact;
        echo "<br/>";
        echo "delaiLivraison => ".$model->delaiLivraison;
        echo "<br/>";
        echo "commercial => ".$model->commercial;
        echo "<br/>";
        echo "valableJusqua => ".$model->valableJusqua;
        echo "<br/>";
        echo "dateRelance => ".$model->dateRelance;
        echo "<br/>";
        echo "etat => ".$model->etat;
        echo "<br/>";
        echo "modePaiement => ".$model->modePaiement;
        echo "<br/>";
        echo "delaiPaiement => ".$model->delaiPaiement;
        echo "<br/>";
        echo "<br/>";
        echo "Reduction 1 : ". $modelDevisReduction1->idReduction;
        echo "Taux de Reduction 1 : ". $modelDevisReduction1->taux;
        echo "<br/>";
        echo "Reduction 2 : ". $modelDevisReduction2->idReduction;
        echo "Taux de Reduction 2 : ". $modelDevisReduction2->taux;
        echo "<br/>";
        echo "Reduction 3 : ". $modelDevisReduction3->idReduction;
        echo "Taux de Reduction 3 : ". $modelDevisReduction3->taux;
        echo "<br/>";
        echo "<br/>";
        echo "<br/>";
        echo "<br/>";
        echo "Articl ".$modelArticleDevis->idArticle;

        die;
        /* $model->save();
        return $this->redirect(['index']); */

    } else {
        return $this->render('create', [
            'model' => $model,
            'modelDevisReduction1' => $modelDevisReduction1,
            'modelDevisReduction2' => $modelDevisReduction2,
            'modelDevisReduction3' => $modelDevisReduction3,
        ]);
    }
}

这是表格: enter image description here

在结果中,我得到了其他人的重复值(Ristourne ID = 3,Taux = 4)

1 个答案:

答案 0 :(得分:1)

问题出在你的代码中。在actionCreate()中,您创建了三个相同类型DevisReduction的模型对象。

$modelDevisReduction1 = new DevisReduction();
$modelDevisReduction2 = new DevisReduction();
$modelDevisReduction3 = new DevisReduction();

在视图中,您已将代码编写为

<?= $form->field($modelDevisReduction1, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-1'] )->label(false); ?>

<?= $form->field($modelDevisReduction2, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-2'] )->label(false); ?>

<?= $form->field($modelDevisReduction3, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-3'] )->label(false); ?>

但是当 $ modelDevisReduction1时,$ modelDevisReduction2和$ modelDevisReduction 引用了三个相同类型的对象 DevisReduction 。该视图将使用name属性{{1对于字段 $ modelDevisReduction1,$ modelDevisReduction2和$ modelDevisReduction3 。因此,当您提交表单时,将会获取最后一个选择框值(字段$ modelDevisReduction3)。这恰好发生在您的案例中。对于输入字段DevisReduction[idReduction]的情况。您可以进一步确认。

  1. 检查渲染的视图html。
  2. 检查表单发布数据。
  3. 我建议您解决上述问题的方法是创建一个自定义表单模型,如下所示。

    DevisReduction[taux]

    您的控制器代码应该是

    <?php
    
    class DevisReductionForm extends yii\base\Model {
    
        public $idReduction1;
        public $idReduction2;
        public $idReduction3;
        public $taux1;
        public $taux2;
        public $taux3;
    
        /**
         * @inheritdoc
         */
        public function rules() {
            $rules = [
                        [['idReduction1', 'idReduction1', 'idReduction1', 'taux1', 'taux2', 'taux3'], 'required'],
            ];
    
            return array_merge($rules, parent::rules());
        }
    
    }
    

    在你看来

    public function actionCreate()
    {
        //create object of custom form model DevisReductionForm given above
        $modelDeviceReduction = new DevisReductionForm ();
    
        if ($modelDeviceReduction->load(Yii::$app->request->post())
        {
             $modelDevisReduction1 = new DevisReduction();
             $modelDevisReduction2 = new DevisReduction();
             $modelDevisReduction3 = new DevisReduction();
    
             $modelDevisReduction1->idReduction =  $modelDeviceReduction->idReduction1
             $modelDevisReduction2->idReduction =  $modelDeviceReduction->idReduction2
             $modelDevisReduction3->idReduction =  $modelDeviceReduction->idReduction3
    
             $modelDevisReduction1->taux = $modelDeviceReduction->taux1
             $modelDevisReduction2->taux = $modelDeviceReduction->taux2
             $modelDevisReduction3->taux = $modelDeviceReduction->taux3
              //write other codes
    
    
        }
        else
        {
            return $this->render('create', [
                'modelDeviceReduction ' => $modelDeviceReduction ,
    
            ]);
        }
    }
    

    希望你明白这一点。