Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
edenramoneda opened this issue Sep 20, 2019 · 2 comments
Open

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

edenramoneda opened this issue Sep 20, 2019 · 2 comments

Comments

@edenramoneda
Copy link

edenramoneda commented Sep 20, 2019

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
`

'dynamicform_wrapper', // 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' ], ]); ?>
<div class="panel panel-default">
    <div class="panel-heading">
        <i class="fa fa-user"></i> Add Official
        <button type="button" class="pull-right add-item btn btn-success btn-xs"><i class="fa fa-plus"></i> Add Official</button>
        <div class="clearfix"></div>
    </div>
    <div class="panel-body container-items"><!-- widgetContainer -->
        <?php foreach ($model as $index => $officialsinfo): ?>
                <div class="item panel panel-default"><!-- widgetBody -->
                    <div class="panel-heading">
                        <span class="panel-title-address">Official: <?= ($index + 1) ?></span>
                        <button type="button" class="pull-right remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
                        <div class="clearfix"></div>
                    </div>
                    <div class="panel-body">
                        <?php
                         if (!$officialsinfo->isNewRecord) {
                                echo Html::activeHiddenInput($officialsinfo, "[{$index}]id");
                            }
                      /*  elseif(!$tbl_official_politicalInfo->isNewRecord){
                            echo Html::activeHiddenInput($tbl_official_politicalInfo, "[{$key}]official_id");
                        }*/
                        ?>
                        <?= $form->field($officialsinfo, "[{$index}]firstname")->textInput() ?>
                        <?= $form->field($officialsinfo, "[{$index}]middlename")->textInput() ?>
                        <?= $form->field($officialsinfo, "[{$index}]lastname")->textInput() ?>
                        <?= 
                            $form->field($officialsinfo, "[{$index}]suffix_id")->widget(Select2::classname(), [
                                'hideSearch' => true,
                                'data' => $suffix,
                                'options' => ['placeholder' => 'Select Suffix', 'required'],
                                'pluginOptions' => [
                                    'allowClear' => true
                                ],
                            ]); 
                            ?>
                        <?= 
                            $form->field($officialsinfo, "[{$index}]birthday")->widget(DatePicker::classname(), [
                                'options' => ['placeholder' => 'Enter birth date ...'],
                                'pluginOptions' => [
                                    'autoclose' => true,
                                    'format' => 'yyyy-mm-dd'
                                ]
                            ]);
                        ?>
                        <?= 
                            $form->field($officialsinfo, "[{$index}]gender_id")->widget(Select2::classname(), [
                                'hideSearch' => true,
                                'data' => $gender,
                                'options' => ['placeholder' => 'Select Sex'],
                                'pluginOptions' => [
                                    'allowClear' => true
                                ],
                            ]);
                        ?>
                        <?= 
                            $form->field($officialsinfo, "[{$index}]gov_position_id")->widget(Select2::classname(), [
                                'hideSearch' => true,
                                'data' => $govpos,
                                'options' => ['class' => 'gov_position_id', 'placeholder' => 'Select Position'],
                                'pluginOptions' => [
                                    'allowClear' => true
                                ],
                            ]); 
                        ?>
        <?php endforeach; ?>
    </div>
</div>
<?php DynamicFormWidget::end(); ?>
<div class="form-group">
    <?= Html::submitButton($officialsinfo->isNewRecord  ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>`
@HarishSTOnline
Copy link

Can you please post the Controller Code For This?

@edenramoneda
Copy link
Author

edenramoneda commented Sep 30, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants