wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
436 stars 438 forks source link

Duplicate entry for key 'PRIMARY' when submit data #298

Open edenramoneda opened 5 years ago

edenramoneda commented 5 years ago

Please help me. It return duplicate entry 74 for key 'PRIMARY' when I cloned the form and tried to submit data. How can I solve this?

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '74' for key 'PRIMARY' The SQL being executed was: INSERT INTO tbl_officials_info (firstname, middlename, lastname, suffix_id, birthday, gender_id, gov_position_id, id) VALUES ('rrere', 'ererer', 'erer', 3, '2019-01-22', 2, 2, 74).

Here is my code `
<?php DynamicFormWidget::begin([ 'widgetContainer' => 'dynamicformwrapper', // required: only alphanumeric characters plus "" [A-Za-z0-9_] 'widgetBody' => '.container-items', // required: css class selector 'widgetItem' => '.item', // required: css class 'limit' => 5, // 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' => $model[0], // 'model' => $tbl_official_politicalInfo[0], 'formId' => 'dynamic-form', 'formFields' => [ 'firstname', 'middlename', 'lastname', 'suffix_id', 'birthday', 'gender_id', 'gov_position_id', 'region_id', 'province_id', 'citymun_id', 'political_party_id', 'proclamation_date' ], ]); ?>

Add Official
$officialsinfo): ?>
Official:
isNewRecord) { echo Html::activeHiddenInput($officialsinfo, "[{$index}]id"); } /* elseif(!$tbl_official_politicalInfo->isNewRecord){ echo Html::activeHiddenInput($tbl_official_politicalInfo, "[{$key}]official_id"); }*/ ?> field($officialsinfo, "[{$index}]firstname")->textInput() ?> field($officialsinfo, "[{$index}]middlename")->textInput() ?> field($officialsinfo, "[{$index}]lastname")->textInput() ?> field($officialsinfo, "[{$index}]suffix_id")->widget(Select2::classname(), [ 'hideSearch' => true, 'data' => $suffix, 'options' => ['placeholder' => 'Select Suffix', 'required'], 'pluginOptions' => [ 'allowClear' => true ], ]); ?> field($officialsinfo, "[{$index}]birthday")->widget(DatePicker::classname(), [ 'options' => ['placeholder' => 'Enter birth date ...'], 'pluginOptions' => [ 'autoclose' => true, 'format' => 'yyyy-mm-dd' ] ]); ?> field($officialsinfo, "[{$index}]gender_id")->widget(Select2::classname(), [ 'hideSearch' => true, 'data' => $gender, 'options' => ['placeholder' => 'Select Sex'], 'pluginOptions' => [ 'allowClear' => true ], ]); ?> field($officialsinfo, "[{$index}]gov_position_id")->widget(Select2::classname(), [ 'hideSearch' => true, 'data' => $govpos, 'options' => ['class' => 'gov_position_id', 'placeholder' => 'Select Position'], 'pluginOptions' => [ 'allowClear' => true ], ]); ?>
isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
`
HarishSTOnline commented 5 years ago

Can you please post the Controller Code For This?

edenramoneda commented 5 years ago

Can you please post the Controller Code For This?


public function actionCreate() 
        {
            $model= [new TblOfficialsInfo]; // $modelCustomer
            $tbl_official_politicalInfo = [new TblOfficialsPoliticalInfo]; //$modelsAddress
            $flag = '';
           // if ($model->load(Yii::$app->request->post()) && $tbl_official_politicalInfo->load(Yii::$app->request->post())) {

                $modelsTblOfficialInfo = MultipleModel::createMultiple(TblOfficialsInfo::classname());
                MultipleModel::loadMultiple($modelsTblOfficialInfo, Yii::$app->request->post());

                $modelsTblOfficialPoliticalInfo = MultipleModel::createMultiple(TblOfficialsPoliticalInfo::classname());
                MultipleModel::loadMultiple($modelsTblOfficialPoliticalInfo, Yii::$app->request->post());
                // validate all models
                $valid = MultipleModel::validateMultiple($modelsTblOfficialInfo) && MultipleModel::validateMultiple($modelsTblOfficialPoliticalInfo);

                //echo print_r($tbl_official_politicalInfo['region_id']);
                foreach($tbl_official_politicalInfo as $test){
                    $official_id = $test->official_id;
                    $region_id = $test->region_id;
                }
                if ($valid) {
                    $transaction = \Yii::$app->db->beginTransaction();

                    try {
                            foreach ($modelsTblOfficialInfo as $modelsTblOfficialInfo) {
                                $modelsTblOfficialInfo->id = Yii::$app->db->getLastInsertID();
                                if (! ($flag = $modelsTblOfficialInfo->save())) {
                                    $transaction->rollBack();
                                    break;
                                }
                            }
                            foreach ($modelsTblOfficialPoliticalInfo as $modelsTblOfficialPoliticalInfo) {
                                $modelsTblOfficialPoliticalInfo->official_id = $modelsTblOfficialInfo->id;
                                if (! ($flag = $modelsTblOfficialPoliticalInfo->save())) {
                                    $transaction->rollBack();
                                    break;
                                }
                            }

                        if ($flag) {
                            $transaction->commit();
                            return $this->redirect(['view', 'id' => $modelsTblOfficialInfo->id]);
                        }
                    } catch (Exception $e) {
                        $transaction->rollBack();
                    }
                }```