wbranca dynagrid 更新错误 [英] Error in wbranca dynagrid update

查看:19
本文介绍了wbranca dynagrid 更新错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wbranca yii2 创建动态表单,但更新操作返回错误

 array_combine():两个参数应该有相同数量的元素

这是更新的表格

 

<?php DynamicFormWidget::begin(['widgetContainer' =>'dynamicform_wrapper',//必需:只有字母数字字符加_" [A-Za-z0-9_]'widgetBody' =>'.container-items',//必需:css 类选择器'widgetItem' =>'.item',//必需:css 类'限制' =>10,//最大次数,一个元素可以被克隆(默认999)'分钟' =>1,//0 或 1(默认 1)'插入按钮' =>'.add-item',//css 类'删除按钮' =>'.remove-item',//css 类'模型' =>$modelsPrItem[0],'formId' =>'动态形式','formFields' =>['po_item_no','数量',],]);?><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 class="clearfix"></div>

<div class="panelf-body"><?php//更新操作所必需的.如果(!$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 class="col-md-2"><?= $form->field($modelPrItem, "[{$i}]Unit_Price")->textInput(['maxlength' => 128]) ?>

<div class="col-md-2"><?= $form->field($modelPrItem, "[{$i}]Extended_price")->textInput(['maxlength' => 128]) ?>

<div class="col-md-2"><?= $form->field($modelPrItem, "[{$i}]Currency_ID")->dropDownList(ArrayHelper::map(Tblcurrency::find()->all(),'CurrencyID','currency_symbol'),[]);?>

<div class="col-md-4"><?= $form->field($modelPrItem, "[{$i}]Description")->textArea(['maxlength' => 128]) ?>

<!-- .row -->

<?php endforeach;?>

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

这是模型:

formName();$post = Yii::$app->request->post($formName);$models = [];如果(!空($multipleModels)){$keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));$multipleModels = array_combine($keys, $multipleModels);}如果 ($post && is_array($post)) {foreach ($post as $i => $item) {if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {$models[] = $multipleModels[$item['id']];} 别的 {$models[] = 新的 $modelClass;}}}取消设置($model,$formName,$post);返回 $models;}

}

当我运行更新时,尤其是在更新多个项目时,上面会返回一个错误

解决方案

错误信息说 $keys 和 $values(别名 $multipleModels )中的元素数量不一样所以你正在尝试构建一个关联数组使用错误的键对 => 值元素

尝试 var_dump(或使用 xdebug 检查)$keys 和 $multipleModels 的内容,并根据您的实际需要调整代码.

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

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

解决方案

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

这篇关于wbranca dynagrid 更新错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆