Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Am using wbranca yii2 to create dynamic forms but the update action returns an error of

    array_combine(): Both parameters should have an equal number of elements

This is the form for the update

                            <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' => 10, // 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' => $modelsPrItem[0],
                            'formId' => 'dynamic-form',
                            'formFields' => [
                                'po_item_no',
                                'quantity',
                            ],
                        ]); ?>

                                <div class="container-items">
                                    <!-- widgetContainer -->
                                    <?php foreach ($modelsPrItem as $i => $modelPrItem): ?>
                                        <div class="item paneld">
                                            <!-- widgetBody -->
                                            <div class="panelf-heading">
                                                <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="panelf-body">
                                                <?php
                                        // necessary for update action.
                                        if (! $modelPrItem->isNewRecord) {
                                            echo Html::activeHiddenInput($modelPrItem, "[{$i}]PRID");
                                        }
                                    ?>
                                                    <div class="row">
                                                        <div class="col-md-2">
                                                            <?= $form->field($modelPrItem, "[{$i}]Quantity")->textInput(['maxlength' => 128]) ?>
                                                        </div>
                                                        <div class="col-md-2">
                                                            <?= $form->field($modelPrItem, "[{$i}]Unit_Price")->textInput(['maxlength' => 128]) ?>
                                                        </div>
                                                        <div class="col-md-2">
                                                            <?= $form->field($modelPrItem, "[{$i}]Extended_price")->textInput(['maxlength' => 128]) ?>
                                                        </div>
                                                        <div class="col-md-2">
                                                            <?= $form->field($modelPrItem, "[{$i}]Currency_ID")->dropDownList(
                                             ArrayHelper::map(Tblcurrency::find()->all(),'CurrencyID','currency_symbol'),[]
                                            ); ?>
                                                        </div>
                                                        <div class="col-md-4">
                                                            <?= $form->field($modelPrItem, "[{$i}]Description")->textArea(['maxlength' => 128]) ?>
                                                        </div>
                                                    </div>
                                                    <!-- .row -->
                                            </div>
                                        </div>
                                        <?php endforeach; ?>
                                </div>
                                <?php DynamicFormWidget::end(); ?>
                        </div>

This is the model:

<?php

   namespace appmodels;

       use Yii;
         use yiihelpersArrayHelper;

          class Model extends yiiaseModel
       {
       /**
     * 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);
    }

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

}

The above returns an error when i run update especially when updating more than one item

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
288 views
Welcome To Ask or Share your Answers For Others

1 Answer

The error message say that the number of element in $keys and $values (alias $multipleModels ) are not the same so you are trying to build an associative array witth a wrong pair of key => value elements

Try var_dump (or inspect with xdebug) the content of the $keys and$multipleModels and adapt the code to your real need .

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...