yii dynamic form两个参数应该具有相同数量的元素

时间:2017-11-06 16:08:59

标签: php yii2 dynamicform

我已经安装了wbraganca / yii2-dynamicform小部件,我的创建功能运行良好,但我正在尝试更新表单,它给了我错误:“array_combine():两个参数应该具有相同数量的元素”从这行代码:

if (! empty($multipleModels)) {
        $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));

             --> this line is causing the error:--> $multipleModels = `array_combine($keys, $multipleModels)`

让我分享一下我的controlelr update和model.php

这是我的更新代码:

 public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $modelsPoItem = $model->addressess;

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

            $oldIDs = ArrayHelper::map($modelsPoItem, 'id', 'id');
            $modelsPoItem = Model::createMultiple(Poitem::classname(), $modelsPoItem);
            Model::loadMultiple($modelsPoItem, Yii::$app->request->post());
            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPoItem, 'id', 'id')));


            // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelsPoItem) && $valid;

            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        if (! empty($deletedIDs)) {
                            Poitem::deleteAll(['id' => $deletedIDs]);
                        }
                        foreach ($modelsPoItem as $modelPoItem) {
                           $modelPoItem->id = $model->id;
                            if (! ($flag = $modelPoItem->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['view', 'id' => $model->id]);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        }

        return $this->render('update', [
            'model' => $model,
            'modelsPoItem' => (empty($modelsPoItem)) ? [new Poitem] : $modelsPoItem,

        ]);
    }

以下是错误产生的模型:

<?php

namespace app\models;

use Yii;
use yii\helpers\ArrayHelper;

class Model extends \yii\base\Model
{
    /**
     * Creates and populates a set of models.
     *
     * @param string $modelClass
     * @param array $multipleModels
     * @return array
     */
    public static function createMultiple($modelClass, $multipleModels = [])
    {
        $model    = new $modelClass;
        $formName = $model->formName();
        $post     = Yii::$app->request->post($formName);
        $models   = [];

        if (! empty($multipleModels)) {
            $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));

                  $multipleModels = array_combine($keys, $multipleModels); 
            // $multipleModels = array_combine($keys, $multipleModels);

        }

        if ($post && is_array($post)) {
            foreach ($post as $i => $item) {
                if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {
                    $models[] = $multipleModels[$item['id']];
                } else {
                    $models[] = new $modelClass;
                }
            }
        }

        unset($model, $formName, $post);

        return $models;
    }
}

最后,让我向您呈现我的观点:

 <div class="col-sm-4">
     <?= $form->field($model, 'gallamay')->textInput() ?>
     <?= $form->field($model, 'gallashart')->textInput() ?>
     <?= $form->field($model, 'galla_given')->textInput() ?>

     <?= $form->field($model, 'galla_done')->widget(SwitchInput::classname(), [
         'pluginOptions'=>[
             'handleWidth'=>70,
             'onText'=>'Бажарилди',
             'offText'=>'Бажарилмади',
             'onColor' => 'primary',
             'offColor' => 'danger',
             'size' => 'mini',
             'display'=>'none',
         ]
     ]);
     ?>



<?= $form->field($model,'date')->widget(DatePicker::className(),['dateFormat' => 'php:Y-m-d'])->textInput(['placeholder' => 'Дата']); ?>








</div>

    </div>
    <div class="col-sm-4">


        <div class="col-sm-4">

        </div>
    </div>


    <div class="row">


        <div class="panel panel-default">
            <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Qo'shimcha dexqonchiliklar</h4></div>
            <div class="panel-body">
                <?php DynamicFormWidget::begin([
                    'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
                    'widgetBody' => '.container-items', // required: css class selector
                    'widgetItem' => '.item', // required: css class
                    'limit' => 4, // the maximum times, an element can be cloned (default 999)
                    'min' => 1, // 0 or 1 (default 1)
                    'insertButton' => '.add-item', // css class
                    'deleteButton' => '.remove-item', // css class
                    'model' => $modelsPoItem[0],
                    'formId' => 'dynamic-form',
                    'formFields' => [
                        'po_item_no',
                        'quantity',


                    ],
                ]); ?>














                <div class="container-items"><!-- widgetContainer -->
                    <?php foreach ($modelsPoItem as $i => $modelsPoItem): ?>
                        <div class="item panel panel-default"><!-- widgetBody -->
                            <div class="panel-heading">
                                <h3 class="panel-title pull-left">Dexqonchilik turlari</h3>
                                <div class="pull-right">
                                    <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
                                    <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                                </div>
                                <div class="clearfix"></div>
                            </div>
                            <div class="panel-body">
                                <?php
                                // necessary for update action.
                                if (! $modelsPoItem->isNewRecord) {
                                    echo Html::activeHiddenInput($modelsPoItem, "[{$i}]id");
                                }
                                ?>

                                   <?php
          $data=\yii\helpers\ArrayHelper::map(\app\models\Extraagri::find()->all(),'id','name');
          ?>

                                <div class="row">
                                    <div class="col-sm-3">
                                        <?= $form->field($modelsPoItem, "[{$i}]po_item_no")->dropDownList($data) ?>
                                    </div>
                                    <div class="col-sm-3">
                                        <?= $form->field($modelsPoItem, "[{$i}]quantity")->textInput(['maxlength' => true]) ?>
                                    </div>
                                     <div class="col-sm-3">
                                        <?= $form->field($modelsPoItem, "[{$i}]olchov")->textInput(['maxlength' => true]) ?>
                                    </div>
                                    <div class="col-sm-3">
                                        <?= $form->field($modelsPoItem, "[{$i}]yer")->textInput(['maxlength' => true])->label('Yer (gektarda)') ?>
                                    </div>
                                </div><!-- .row -->

                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>

                <?php DynamicFormWidget::end(); ?>
            </div>
        </div>









        <div class="form-group">
            <?= Html::submitButton($model->isNewRecord ? 'Кўшиш' : 'Тахрирлаш', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

        </div>









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

0 个答案:

没有答案