0

最近我安装了 multiSelect 复选框扩展。我的 _form.php 看起来像这样:

<? = $ Form-> field ($ model_detail, 'product_id') ->
widget (MultiSelect :: className (),
['id' => "multiXX", 
"options" => ['multiple' => "multiple"],
'data' => $ rows,
'attribute' => 'product_id', 
'model' => $ models , 
"clientOptions" =>
[
"includeSelectAllOption" => true, 'numberDisplayed' => 2,
],
]);

如何提取用户在另一个模型的 actionCreate 中选择的数据?如何获得用户选择的选项?(我尝试了 $_post[] 并没有用。)

4

1 回答 1

0

这是整个表格:

   div class="customer-order2-form">


    <?php $form = ActiveForm::begin(['action' => ['create']]); ?>

    <?= $form->field($model, 'customer_name')->textInput(['maxlength' => 255]) ?>
    <?= $form->field($model, 'customer_phone')->widget(\yii\widgets\MaskedInput::className(), 
    [
    'mask' => '999-9999999',
     'clientOptions' => ['alias' =>  '972']
]) ?>

    <?php /* $form->field($model, 'customer_phone')->textInput(['maxlength' => 255]) **/?> 

    <?= $form->field($model, 'order_date')->widget(
    DatePicker::className(), [
         'inline' => false, 
      // 'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
        'clientOptions' => [
            'autoclose' => true,
            'format' => 'yyyy-m-d'
        ]
]);?>


<?php  $model_detail = new OrderDetails2(); /* -------- כאן הוספתי נתונים ממודל 
$rows=ArrayHelper::map(ProductFamily::find()->all(), 'id', 'name');  //--- אין צורך בשאילתא, עכשיו גם מציג את מה שנבחר למעלה
?>

 <?= $form->field($model_detail, 'product_id')->widget( 
 MultiSelect::className(), [
    'id'=>"multiXX",
    "options" => ['multiple'=>"multiple"], // for the actual multiselect
    'name' =>'multicheck',
    'data' =>$rows,
    'attribute' => 'product_id', // if preselected
    'model' => $models, 
    "clientOptions" => 
        [
            "includeSelectAllOption" => true,
            'numberDisplayed' => 2,
        ], 
]);
?>

<?php

echo $form->field($model, 'description')->textarea(['rows' => 6]);
echo /*$form->field($model, 'totalprice')->textInput();*/
 $form->field($model, 'totalprice')->widget(MaskMoney::classname(), [
    'pluginOptions' => [
        'prefix' => '₪ ',
        'allowNegative' => false
    ]
]);

echo $form->field($model, 'status_id')->dropDownList(ArrayHelper::map(Status::find()->select('*')->all(), 'id', 'name'));

?>

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'יצירה' : 'עדכון', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>

</div>

我尝试的是使用默认数组保存到数据库中的不同表中,并且它有效。

它看起来像这样:

public function actionCreate()
    {
        $model = new CustomerOrder2();
        $model_details= new OrderDetails2();

    if ($model->load(Yii::$app->request->post()) && $model->save()) 
    {

if (isset($_POST['product_id'])){
     $model_details->attributes = $_POST['product_id'];
  $model_details->attributes=array();
echo 
$model_details->attributes;
}

echo $_POST['description'];
    $values =  Array('1'=>'1','2'=>'2');
    $model_details->attributes = $_POST['product_id'];
    $array=$model_details->attributes;

    //$data=OrderDetails2::->request->post();
    //$values= Array(Yii::$app->request->post()['multiXX']);
    //$values=Array($model_details->attributes);

    foreach($values as $value)
    {
        $command = Yii::$app->db->createCommand();
        $command->insert('order_details',array('Order_id'=>$model->id,
                            'product_id'=> $value,
                            'quantityOrdered'=> '1',
                            ));
        $command->execute();                    
       }

        return $this->redirect(['view', 'id' => $model->id]);
        } 

        else {
            return $this->render('create', [
                'model' => $model,

            ]);
        }
    }
于 2015-09-02T17:44:25.403 回答