Open edenramoneda opened 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).
tbl_officials_info
firstname
middlename
lastname
suffix_id
birthday
gender_id
gov_position_id
id
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' ], ]); ?>
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(); } }```
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' ], ]); ?>
Can you please post the Controller Code For This?