Skip to content
New issue

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

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

Already on GitHub? # to your account

Cant save in data in database using dynamic form in one model #331

Open
devansh-gtu opened this issue Jan 30, 2023 · 0 comments
Open

Cant save in data in database using dynamic form in one model #331

devansh-gtu opened this issue Jan 30, 2023 · 0 comments

Comments

@devansh-gtu
Copy link

Controller:
public function actionCreate()
{

    $model = new UniversityCourseList;
    $Course= [new UniversityCourseList];
    $countries = ArrayHelper::map(Country::getAllCountries(), 'id', 'name');

    if ($this->request->isPost) 
    {
        if ($model->load($this->request->post()))
        {
            echo "hiii";
            die(); 
            $Course = Model::createMultiple(UniversityCourseList::classname());
            Model::loadMultiple($Course, Yii::$app->request->post());

        // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($Course) && $valid;

            if ($valid) 
            {
                $transaction = \Yii::$app->db->beginTransaction();

                try {
                    if ($flag = $model->save(false)) {
                        foreach ($Course as $Course) {
                            $Course->customer_id = $model->id;
                            if (!($flag = $Course->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }

                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect('index');
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        }
    }
    
    //  if ($model->load(Yii::$app->request->post())) 
    //  {
          
    //     $Course = Model::createMultiple(UniversityCourseList::classname());
    //     Model::loadMultiple($Course, Yii::$app->request->post());

    //     // validate all models
    //     $valid = $model->validate();
    //     $valid = Model::validateMultiple($Course) && $valid;

    //     if ($valid) 
    //     {
    //         $transaction = \Yii::$app->db->beginTransaction();

    //         try {
    //             if ($flag = $model->save(false)) {
    //                 foreach ($Course as $Course) {
    //                     $Course->customer_id = $model->id;
    //                     if (!($flag = $Course->save(false))) {
    //                         $transaction->rollBack();
    //                         break;
    //                     }
    //                 }
    //             }

    //             if ($flag) {
    //                 $transaction->commit();
    //                 return $this->redirect('index');
    //             }
    //         } catch (Exception $e) {
    //             $transaction->rollBack();
    //         }
    //     }
    // }




         return $this->render('create', [
            'model' => $model,
            'courseType' => $this->getOthers('course_type'),
            'languages' => $this->getOthers('languages'),
            'intake' => $this->getOthers('intake'),
            'durationType' =>$this->getOthers('duration_type'),             
            'university' => $this->getUniversityList(),
            'countries' => $countries,
            'degree' => Degree::getAllDegrees(),
            'majors' => Majors::getAllMajors(),  
            'degreeLevels' => DegreeLevel::getAllDegreeLevels(), 
            'Course' => (empty($Course)) ? [new UniversityCourseList] : $Course, 
        ]);
}

form:

registerJsFile('@web/js/programs.js'); $this->registerJsFile('@web/libs/select2/select2.full.min.js'); $this->registerCssFile('@web/libs/select2/select2.min.css'); $year=[0=>' ',1=>'Janurary',3=>'Feburary',3=>'March',4=>'April',5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',10=>'October',11=>'November',12=>'December']; ?>
'dynamic-form']); ?>
<div class="padding-v-md">
    <div class="line line-dashed"></div>
</div>
<?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
    'min' => 1, // 0 or 1 (default 1)
    'insertButton' => '.add-item', // css class
    'deleteButton' => '.remove-item', // css class
    'model' =>  $Course[0],
    'formId' => 'dynamic-form',
    'formFields' => [
        'name',
        'university_id',
        'degree_id',
        'degree_level_id',
        'intake',
        'deadline1',
        'intake2',
        'deadline2',
        'placement_fess',
        'a_level',
        'ib_level',
        'fees_international_students',
        'type',
    ],
]); ?>
<div class="panel panel-default">
    <div class="panel-heading">
        <i class="fa fa-envelope"></i> 
        <button type="button" class="pull-right add-item btn btn-success btn-xs"><i class="fa fa-plus"></i> Add Patner University</button>
        <div class="clearfix"></div>
    </div>
    <div class="panel-body container-items"><!-- widgetContainer -->
        <?php foreach ($Course as $index => $Course): ?>
            <div class="item panel panel-default"><!-- widgetBody -->
                <div class="panel-heading">
                    <span class="panel-title-address">Patner University: <?= ($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
                        // necessary for update action.
                        if (!$Course->isNewRecord) {
                            echo Html::activeHiddenInput($Course, "[{$index}]id");
                        }
                    ?>
                     <div class="row">
                        <div class="col-sm-6">
                             <?= $form->field($Course, "[{$index}]name")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-6">
                           <?= $form->field($Course, "[{$index}]university_id")->dropDownList($university, ['prompt' => 'Select University']) ?>
                        </div>
                        <div class="col-sm-6">
                             <?= $form->field($Course, "[{$index}]degree_id")->dropDownList($degree, ['prompt' => 'Select Discipline','id'=>'degree_id']) ?>
                        </div>
                        <div class="col-sm-6">
                              <?= $form->field($Course, "[{$index}]degree_level_id")->dropDownList($degreeLevels, ['prompt' => 'Select Degree']) ?>
                        </div>
                    </div><!-- .row -->
                    <div class="row">
                        <div class="col-sm-4">
                            <?= $form->field($Course, "[{$index}]intake")->dropDownList($year)?>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($Course, "[{$index}]deadline1")->widget(DatePicker::classname(), ['options' => ['placeholder' => 'Select date'],'pluginOptions' => ['format' => 'mm-dd','todayHighlight' => true]]);?>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($Course, "[{$index}]intake2")->dropDownList($year) ?>
                        </div>
                        <div class="col-sm-4">
                           <?= $form->field($Course, "[{$index}]deadline2")->widget(DatePicker::classname(), ['options' => ['placeholder' => 'Select date'],'pluginOptions' => ['format' => 'mm-dd','todayHighlight' => true]]);?>
                        </div>
                        <div class="col-sm-4">
                             <?= $form->field($Course, "[{$index}]placement_fess")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($Course, "[{$index}]a_level")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($Course, "[{$index}]ib_level")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-4">
                           <?= $form->field($Course, "[{$index}]fees")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-4">
                           <?= $form->field($Course, "[{$index}]fees_international_students")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($Course, "[{$index}]type")->dropDownList($courseType,['prompt' => 'Select Type']) ?>
                        </div>
                    </div><!-- .row -->
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    <?php DynamicFormWidget::end(); ?>
</div>
 <div class="form-group">
    <?= Html::submitButton($Course->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
</div>
<script> function updateAdmission(element) { console.log(element); var degree = element.value; if (degree !== null && degree !== undefined && degree !== "") { $.ajax({ url: '/backend/web/index.php?r=university/dependent-courses', method: 'POST', data: { 'degree': degree, 'university': "id ?>" }, success: function(response) { response = JSON.parse(response); if(response.status == "success") { var row = element.id.split('-')[1]; var select = $('#universityadmission-' + row + '-course_id'); select.empty(); var data = response.result; for(var i = 0; i < data.length; i++) { select.append( '' + data[i].name + '' ); } } }, error: function(){ console.log('error', arguments); } }); } } </script>
@devansh-gtu devansh-gtu changed the title Cant save in data base using dynamic form in one model Cant save in data in database using dynamic form in one model Jan 30, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant