Error in wbranca dynagrid update

拥有回忆 提交于 2019-12-25 19:58:13

问题


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

    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


回答1:


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


来源:https://stackoverflow.com/questions/38768439/error-in-wbranca-dynagrid-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!