php yii 插入,php – 无法在ActiveRecord Yii2中插入数据库

我不知道什么是错的但我无法插入数据库.

这就是我在模型中得到的.

warranty.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,

]);

}

}

并以我的观点/形式

'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] ]); ?>

= $form->field($model, 'item_no',

['addon' => ['append' => ['content'=> '']] ]); ?>

= $form->field($model, 'warranty_date')-> widget(DatePicker::classname(),

[

'options' => ['placeholder' => 'Date Claimed'],

'pluginOptions' => [ 'autoclose'=>true ]

]); ?>

= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'warranty-button']) ?>

= Html::resetButton('Reset', ['class' => 'btn btn-default']); ?>

它没有给出任何错误但是当我在数据库上检查它时,没有添加任何内容.我希望有人能帮助我.谢谢.我是YII2的新手,所以我仍然没有得到它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值