<?php
namespace app\models;
use yii\db\ActiveRecord;
class Test extends ActiveRecord{
public function rules()
{
return [
['title','string','length'=>[0,10]]
];
}
}
控制器代码:
public function actionTest(){
//添加数据
$test = new Test;
$test->title = '';
$test->validate();
if ($test->hasErrors()) {
echo 'error';
} else {
$test->save();
}
}
结论:保存数据及验证数据。 yii 数据库删除数据
public function actionTest(){
//删除
//方法一
$result = Test::find()->where(['id' => 1])->all();
$result[0]->delete();
//方法二
Test::deleteAll('id>:id', array(':id' => 5));
}