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

摘自 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(); // 迭代器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值