yii 之增加数据

模型代码:

<?php
namespace appmodels;
use yiidbActiveRecord;
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();
        }
    }

结论:保存数据及验证数据。

原文地址:https://www.cnblogs.com/gyfluck/p/9101273.html