primary keys in all tables(about 120) of our database are set to AUTO_INCREMENT.
I noticed,that yii2 seems to neglect this guideline. If I use to create a new record (without filling out PrimaryKey in formular, of course) new record will have an id, which is completely without foundation in fact. For instance, I have two records in table, which are created using phpmyadmin by id=1/id=2. New record,created using yii will have id 4191. Furthermore, I sometimes get error :
Duplicate entry for Primary Key
Any ideas,how to fix this probelm?
Here is formular. id (Primay Key) will nowhere be filled out,'cause this value should be saved into database behind the scences!
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use kartik\date\DatePicker; use \common\modules\basis\models\Person; use common\modules\lookup\models\LPersonArt; use kartik\widgets\FileInput; $id_person_art_bewerber = Person::find()->where(['id_person_art' => 1])->one()->id_person_art; $id_person_art_mitarbeiter = Person::find()->where(['id_person_art' => 3])->one()->id_person_art; $form = ActiveForm::begin([ 'id' => 'bewerber-view', 'options' => [ 'class' => 'form-horizontal'] ]); ?> <?= $form->errorSummary($model); ?> <!-- START ACCORDION & CAROUSEL--> <div class="row"> <div class="col-md-6"> <div class="box box-solid"> <div class="box-header with-border"> <h3 class="box-title">Bitte hier zu-und aufklappen </h3> </div> <!-- /.box-header --> <div class="box-body"> <div class="box-group" id="accordion"> <!-- we are adding the .panel class so bootstrap.js collapse plugin detects it --> <div class="panel box box-primary"> <div class="box-header with-border"> <h4 class="box-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"> Inputfelder der Personentabelle </a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse in"> <div class="box-body"> <div class="bewerber-view"> <?= $form->field($model_person, 'angelegt_am')->widget(\kartik\datecontrol\DateControl::classname(), [ 'type' => \kartik\datecontrol\DateControl::FORMAT_DATETIME, 'disabled' => true, ]); ?> . . . <!-- END ACCORDION & CAROUSEL--> <?= $form->field($model, 'id_person')->widget(\kartik\widgets\Select2::classname(), [ 'data' => Person::getPerson($id_person_art_bewerber), 'options' => ['placeholder' => Yii::t('app', 'Bitte geben sie den Nachnamen des Bewerbers über die DropDownbox ein')], 'pluginOptions' => [ 'allowClear' => true, ], ]); ?> . . .
Here is Controller:
public function actionCreate() { //try { $model = new Bewerber(['scenario' => Bewerber::SCENARIO_CREATE]); $model_person = new Person(['scenario' => Person::SCENARIO_CREATE]); $behavior = new \common\wsl_components\Wsl_Blameable_Behavior(); $angelegt_von = $behavior->get_User_Person_Id(); if (!$model || !$model_person) { throw new NotFoundHttpException("Bitte überprüfen Sie die Validität der Tabellen bewerber bzw. person"); } $model->angelegt_von = $angelegt_von; $model_person->angelegt_von = $angelegt_von; if ($model->load(Yii::$app->request->post()) && $model_person->load(Yii::$app->request->post())) { $valid = $model->validate(); $isValid = $model_person->validate(); $isValid = $valid & $isValid; if ($isValid) { $model->avatar = UploadedFile::getInstances($model, 'avatar'); $model->upload(); $model_person->save(); $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { $error_person = $model_person->getErrors(); $error_bewerber = $model->getErrors(); foreach ($error_person as $values) { foreach ($values as $ausgabe) { echo"<p>" . $ausgabe . "</p>"; } } foreach ($error_bewerber as $values) { foreach ($values as $ausgabe) { echo"<p>" . $ausgabe . "</p>"; } } echo Growl::widget([ 'type' => Growl::TYPE_DANGER, 'title' => 'Oh snap!', 'icon' => 'glyphicon glyphicon-remove-sign', 'body' => 'Aus unbekannten Gründen konnten die Tabellen nicht validiert werden<br>Bitte informieren Sie den Hersteller gemäß obiger Angaben!', 'showSeparator' => true, 'delay' => 2000, 'pluginOptions' => [ 'showProgressbar' => true, 'placement' => [ 'from' => 'top', 'align' => 'center', ] ] ]); return $this->render('create', [ 'model' => $model, 'model_person' => $model_person, ]); } } else { return $this->render('create', [ 'model' => $model, 'model_person' => $model_person, ]); } /* } catch (\Exception $error) { $go_back = "bewerber"; \common\wsl_components\error_handling::error($error, $go_back); } */ }
Models have been generated using gii of mootensai. If I should paste it,please tell me!
Maybe, this behavior is 'caused by model using uuid-behavior of mootensai? If I delete this Behavior, I will get error like this
SQLSTATE[22003]: Numeric value out of range: 167 Out of range value for column 'id' at row 1 The SQL being executed was: INSERT INTO `bewerber` (`angelegt_von`, `id_person`, `id_person_rekrutiert_von`, `id_abrechnungsweg`, `id_kanal`, `id_bewerberquelle`, `id_ba_xml_gelernter_beruf_1`, `id_ba_xml_gelernter_beruf_2`, `id_ba_xml_gelernter_beruf_3`, `umkreis`, `arbeitsuchend_seit`, `avgs`, `arbeitsumfang_vollzeit`, `avgs_betrag`, `ablaufdatum_avgs`, `wunschgehalt_brutto`, `beurteilung_fachlich`, `beurteilung_persoenlich`, `sonstiges`, `verfuegbar_ab`, `gesuchte_positionen`, `anschreiben`, `arbeitsumfang_teilzeit`, `arbeitszeit_bueroueblich`, `arbeitszeit_vormittag`, `arbeitszeit_nachmittag`, `arbeitszeit_abend`, `arbeitszeit_nacht`, `arbeitszeit_wochenende`, `schichtbereitschaft`, `wochenstunden_minimum`, `wochenstunden_maximum`, `bemerkung_intern`, `quereinsteiger`, `zeitarbeit`, `pkw`, `fuehrerschein_pkw`, `fuehrerschein_lkw`, `fuehrerschein_omnibus`, `reisebereitschaft`, `kandidat`, `kandidat_seit`, `veroeffentlichen_bewerberboerse`, `kontakt_halten`, `aktiv`, `zuletzt_deaktiviert_am`, `angelegt_am`, `optimistic_lock`, `aktualisiert_am`) VALUES (5, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 10, NULL, 1, 1, 2000, NULL, 750, '', '', '', NULL, '', '', NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, 10, 30, '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, 1, 1, 1, NULL, NOW(), 0, NOW()) Error Info: Array( [0] => 22003 [1] => 167 [2] => Out of range value for column 'id' at row 1
Here is UUID Behavior of mootensai in model:
public function behaviors() { return [ 'timestamp' => [ 'class' => TimestampBehavior::className(), 'createdAtAttribute' => 'angelegt_am', 'updatedAtAttribute' => 'aktualisiert_am', 'value' => new \yii\db\Expression('NOW()'), ], 'uuid' => [ 'class' => UUIDBehavior::className(), 'column' => 'id', ], ]; }
I got solution: I had to remove UUID class of mootensai in both models!!
0 comments:
Post a Comment