framework example español datos createcommand consultas app php mysql activerecord yii2

php - example - no se puede insertar en la base de datos en ActiveRecord Yii2



yii2 español (1)

No sé lo que está mal, pero no puedo insertarlo en la base de datos.

esto es lo que obtuve en el modelo.

warranty.php

<?php namespace app/models; use Yii; use yii/db/ActiveRecord; use yii/behaviors/TimestampBehavior; use yii/db/Expression; use yii/helpers/VarDumper; /** * This is the model class for table "warrantytwo". * * @property integer $id * @property string $item_no * @property string $warranty_date * @property string $date * @property integer $created_by * @property string $tstamp */ class Warrantytwo extends ActiveRecord { /** * @inheritdoc */ public static function tableName() { return ''warrantytwo''; } public function behaviors() { return [ [ ''class'' => TimestampBehavior::className(), ''createdAtAttribute'' => ''tstamp'', ''updatedAtAttribute'' => false, ''value'' => new Expression(''NOW()''), ], ]; } /** * @inheritdoc */ public function rules() { return [ [[''item_no'', ''warranty_date''], ''required''], [[''warranty_date'', ''date''], ''string''], [''created_by'', ''default'', ''value'' => ''none''], [[''created_by''], ''integer''], [[''warranty_date'', ''date'',''tstamp''], ''safe''], [[''item_no''], ''string'', ''max'' => 100] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ ''id'' => ''ID'', ''item_no'' => ''Item No.'', ''warranty_date'' => ''Warranty Date'', ''date'' => ''Date'', ''created_by'' => ''Created By'', ''tstamp'' => ''tstamp'', ]; } public function addWarrantytwo() { if ($this->validate()) { $Warrantytwo = new Warrantytwo(); $Warrantytwo->item_no = $this->item_no; $Warrantytwo->warranty_date = $this->warranty_date; $Warrantytwo->created_by = ''admin1''; $Warrantytwo->date = date(''Y-m-d H:i:s''); //$Warrantytwo->touch(''tstamp''); if ($Warrantytwo->save()) { return $Warrantytwo; }else { VarDumper::dump($Warrantytwo->getErrors()); } } //return null; } }

controller.php

public function actionWarranty() { //$model= new WarrantyDate(); $model= new Warrantytwo(); if ($model->load(Yii::$app->request->post()) && $model->addWarrantytwo()) { return $this->redirect([''warranty'']); } else { return $this->render(''warranty'', [ ''model'' => $model, ]); } }

y en mi punto de vista / formulario

<?php $form = ActiveForm::begin([ ''id'' => ''new-warranty-form'', //''options'' => [''class'' => ''form-horizontal''], //for yii/ActiveForm ''type'' => ActiveForm::TYPE_HORIZONTAL, //for krajee/ActiveForm ''formConfig'' => [''labelSpan'' => 4, ''deviceSize'' => ActiveForm::SIZE_SMALL] ]); ?> <div class="form-horizontal"> <?= $form->field($model, ''item_no'', [''addon'' => [''append'' => [''content''=> ''<a href="#w" onClick="sample()"><i class="glyphicon glyphicon-search"></i></a>'']] ]); ?> <?= $form->field($model, ''warranty_date'')-> widget(DatePicker::classname(), [ ''options'' => [''placeholder'' => ''Date Claimed''], ''pluginOptions'' => [ ''autoclose''=>true ] ]); ?> </div> <div class="col-sm-offset-5 col-md-9"> <?= Html::submitButton(''Submit'', [''class'' => ''btn btn-primary'', ''name'' => ''warranty-button'']) ?> <?= Html::resetButton(''Reset'', [''class'' => ''btn btn-default'']); ?> </div> <?php ActiveForm::end(); ?>

No da ningún error, pero cuando lo compruebo en la base de datos, no se agrega nada. Espero que alguien pueda ayudarme Gracias. Soy nuevo en YII2, así que todavía no lo entiendo.


Dado que debe determinar en qué situaciones ocurre el problema, realice la prueba de esta manera. (A veces crea un poco de confusión porque validate () siempre devuelve algo)

public function addWarrantytwo() { if ($this->validate()) { $Warrantytwo = new Warrantytwo(); $Warrantytwo->item_no = $this->item_no; $Warrantytwo->warranty_date = $this->warranty_date; $Warrantytwo->created_by = ''admin1''; $Warrantytwo->date = date(''Y-m-d H:i:s''); //$Warrantytwo->touch(''tstamp''); if ($Warrantytwo->save(false)) // try save without validation { return $Warrantytwo; }else { VarDumper::dump($Warrantytwo->getErrors()); exit; } } else { VarDumper::dump($this->validate()); // check for the correct flow exit; } //return null; }