I don’t know what is wrong, but I can’t insert into the database.
This is what I got in the model.
warranty.php
<?php namespace app\models; use Yii; use yii\db\ActiveRecord; use yii\behaviors\TimestampBehavior; use yii\db\Expression; use yii\helpers\VarDumper; class Warrantytwo extends ActiveRecord { public static function tableName() { return 'warrantytwo'; } public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'createdAtAttribute' => 'tstamp', 'updatedAtAttribute' => false, 'value' => new Expression('NOW()'), ], ]; } 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] ]; } 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('Ymd H:i:s');
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, ]); } }
and in my views / form
<?php $form = ActiveForm::begin([ 'id' => 'new-warranty-form', <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(); ?>
It does not give any errors, but when I check it in the database, nothing is added. Hope someone can help me. Thanks. I am new to YII2, so I still do not understand.