YII2.0框架(二) 模型类参考yii\base\Model

时间:2022-10-20 20:46:33

摘自 http://www.yiichina.com/doc/api/2.0/yii-base-model

        $model = new Admin();
        $rules = $model->rules(); //验证规则
        /**
         * [
         *     // built-in "required" validator
         *     [['username', 'password'], 'required'],
         *     // built-in "string" validator customized with "min" and "max" properties
         *     ['username', 'string', 'min' => 3, 'max' => 12],
         *     // built-in "compare" validator that is used in "register" scenario only
         *     ['password', 'compare', 'compareAttribute' => 'password2', 'on' => 'register'],
         *     // an inline validator defined via the "authenticate()" method in the model class
         *     ['password', 'authenticate', 'on' => 'login'],
         *     // a validator of class "DateRangeValidator"
         *     ['dateRange', 'DateRangeValidator'],
         * ];
         */

        $scenarios = $model->scenarios(); //场景列表和对应的活动属性
        /**
         * [
         *     'scenario1' => ['attribute11', 'attribute12', ...],
         *     'scenario2' => ['attribute21', 'attribute22', ...],
         *     ...
         * ]
         */

        $formName = $model->formName(); //默认当前模型类名: Admin
        $attributes = $model->attributes(); //默认当前模型所有非公有静态属性
        $attributeLabels = $model->attributeLabels(); //属性标签,用于页面展示
        $attributeHints = $model->attributeHints(); //属性提示,用于页面展示
        $validate = $model->validate(); //验证表单
        $getValidators = $model->getValidators(); //所有的声明在[[rules()]]的验证器
        $getActiveValidators = $model->getActiveValidators(); //当前场景的验证器
        $isAttributeRequired = $model->isAttributeRequired('name'); //属性是否必须
        $isAttributeSafe = $model->isAttributeSafe('name'); //属性是否安全
        $isAttributeActive = $model->isAttributeActive('name'); //属性是否在当前场景
        $getAttributeLabel = $model->getAttributeLabel('name'); //指定属性的标签
        $getAttributeHint = $model->getAttributeHint('name'); //指定属性的提示
        $hasErrors = $model->hasErrors('name'); //是否有错误
        $getErrors = $model->getErrors(); //所有属性或单个属性的错误
        $getFirstErrors = $model->getFirstErrors(); //所有属性的第一条错误
        /**
         * Array
         *   (
         *      [name] => Name cannot be blank.
         *      [phone] => Phone cannot be blank.
         *      [password] => Password cannot be blank.
         *      [repassword] => Repassword cannot be blank.
         *  )
         */

        $getFirstError = $model->getFirstError('name');//属性的第一条错误: Name cannot be blank.
        $model->addError('name', '名称不能为空!'); //给属性添加错误
        $getNameErrors = $model->getErrors('name');
        /**
         * Array
         *   (
         *       [0] => Name cannot be blank.
         *       [1] => 名称不能为空!
         *   )
         */

        $model->clearErrors('name'); //清除错误
        $generateAttributeLabel = $model->generateAttributeLabel('name'); // 通过给定的属性名生成一个友好的属性标签
        $getAttributes = $model->getAttributes(['name', 'phone']); // 获得属性值
        $model->setAttributes(['name'=>'jack', 'phone'=>'1500']); // 批量设置属性值
        $getAttributes = $model->getAttributes(['name', 'phone']); // 获得属性值
        $getScenario = $model->getScenario(); // 当前场景
        // $model->setScenario('add'); //设置当前场景
        $safeAttributes = $model->safeAttributes(); // 当前场景的安全属性
        /**
         * Array
         *  (
         *    [0] => name
         *    [1] => phone
         *    [2] => password
         *    [3] => repassword
         *  )
         */

        $activeAttributes = $model->activeAttributes(); // 当前场景的验证属性
        $load = $model->load($post); // 将输入的数据填入模型
        $loadMultiple = Admin::loadMultiple([$model], $post); // 从终端用户向模型中填入数据
        $validateMultiple = Admin::validateMultiple([$model]); // 验证多种模型
        $fields = $model->fields(); // 字段列表
        /** Array
         *   (
         *       [name] => name
         *       [phone] => phone
         *       [password] => password
         *       [repassword] => repassword
         *   )
         */

        $getIterator = $model->getIterator(); // 迭代器