使用wbranca yii2创建动态表单但更新操作返回错误
array_combine(): Both parameters should have an equal number of elements
这是更新的表格
<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>
这是模型:
<?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;
}
}
当我运行更新时,尤其是在更新多个项目时,上面会返回错误
答案 0 :(得分:0)
错误消息说$ keys和$ values(别名$ multipleModels)中的元素数量不一样,所以你试图用一对错误的key =&gt;构建一个关联数组。价值元素
尝试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);
}