Model类,集中整个应用的数据和业务逻辑——场景、属性和标签:

  /**
      * Returns a list of scenarios and the corresponding active attributes.
      * An active attribute is one that is subject to validation in the current scenario.
      * 返回所有场景及与之对应的 active 属性的列表
      * active 属性是指在默认场景中验证的
      * The returned array should be in the following format:
      * 返回的格式如下:
      * ```php
      * [
      *     'scenario1' => ['attribute11', 'attribute12', ...],
      *     'scenario2' => ['attribute21', 'attribute22', ...],
      *     ...
      * ]
      * ```
      *
      * By default, an active attribute is considered safe and can be massively assigned.
      * 默认情况下,一个active属性被认为是安全的,可以被批量赋值
      * If an attribute should NOT be massively assigned (thus considered unsafe),
      * 如果不允许批量赋值,则在属性名称前面加“!”
      * please prefix the attribute with an exclamation character (e.g. `'!rank'`).
      *
      * The default implementation of this method will return all scenarios found in the [[rules()]]
      * declaration. A special scenario named [[SCENARIO_DEFAULT]] will contain all attributes
      * found in the [[rules()]]. Each scenario will be associated with the attributes that
      * are being validated by the validation rules that apply to the scenario.
      * 默认情况下会返回[[rules()]]中定义的所有场景,其中[[SCENARIO_DEFAULT]] 默认场景包含所有的属性
      * 每个场景对应的属性值将按指定的验证规则验证
      *
      * @return array a list of scenarios and the corresponding active attributes.
      */
     public function scenarios()
     {
         // 默认情况下有default 的场景
         $scenarios = [self::SCENARIO_DEFAULT => []];
         foreach ($this->getValidators() as $validator) {
             // 遍历validator,取出所有提到的场景,包括 on 和 except
             foreach ($validator->on as $scenario) {
                 $scenarios[$scenario] = [];
             }
             foreach ($validator->except as $scenario) {
                 $scenarios[$scenario] = [];
             }
         }
         // 取出所有场景的名称
         $names = array_keys($scenarios);

         foreach ($this->getValidators() as $validator) {
             if (empty($validator->on) && empty($validator->except)) {
                 // 如果 validator 即没有定义 on,也没有定义 except,就放到所有的场景中
                 foreach ($names as $name) {
                     // 循环 $validator 的所有属性
                     foreach ($validator->attributes as $attribute) {
                         $scenarios[$name][$attribute] = true;
                     }
                 }
             } elseif (empty($validator->on)) {
                 // 如果没有定义 on
                 foreach ($names as $name) {
                     if (!in_array($name, $validator->except, true)) {
                         // 而且场景不在 except 中, 就将这个属性加入到相应的场景中
                         foreach ($validator->attributes as $attribute) {
                             $scenarios[$name][$attribute] = true;
                         }
                     }
                 }
             } else {
                 // 如果定义了 on
                 foreach ($validator->on as $name) {
                     // 就将这个属性加入到 on 定义的场景中
                     foreach ($validator->attributes as $attribute) {
                         $scenarios[$name][$attribute] = true;
                     }
                 }
             }
         }

         /**
          * 将 $scenarios 从
          *
          * ~~~
          * [
          *     'default' => [],
          *     'scenario1' => ['attribute11' => true, 'attribute12' => true, ...],
          *     'scenario2' => ['attribute21' => true, 'attribute22' => true, ...],
          *     'scenario3' => [],
          *     ...
          * ]
          * ~~~
          * 转化为
          * ~~~
          * [
          *     'default' => [],
          *     'scenario1' => ['attribute11', 'attribute12', ...],
          *     'scenario2' => ['attribute21', 'attribute22', ...],
          *     ...
          * ]
          * ~~~
          */
         foreach ($scenarios as $scenario => $attributes) {
             // 去除掉没有属性值的场景
             if (!empty($attributes)) {
                 // 取出场景中的属性名称
                 $scenarios[$scenario] = array_keys($attributes);
             }
         }

         return $scenarios;
     }

     /**
      * Returns the form name that this model class should use.
      *
      * 返回表单的名称,就是这个 model 的类名,类名即表名
      *
      * The form name is mainly used by [[\yii\widgets\ActiveForm]] to determine how to name
      * the input fields for the attributes in a model. If the form name is "A" and an attribute
      * name is "b", then the corresponding input name would be "A[b]". If the form name is
      * an empty string, then the input name would be "b".
      * 表单名称用于[[\yii\widgets\ActiveForm]]确定模型中的属性名对应的输入域的名称
      * 如果表单名为A,属性名为b,对应的输入名为A[b],如果表单名为空,则输入名为b
      *
      * The purpose of the above naming schema is that for forms which contain multiple different models,
      * the attributes of each model are grouped in sub-arrays of the POST-data and it is easier to
      * differentiate between them.
      * 上述命名模式的目的是,对于包含多个不同模型的表单,每个模型的属性分组在对应的表单名下的数组,能更好的区分它们
      *
      * By default, this method returns the model class name (without the namespace part)
      * as the form name. You may override it when the model is used in different forms.
      * 默认情况下该方法返回不包含命名空间的类名称,可以通过重写使用别的表名
      *
      * @return string the form name of this model class.
      * @see load()
      */
     public function formName()
     {
         // ReflectionClass 类,是php内置的反射类,用于获取类的信息,包含了一个类的有关信息
         $reflector = new ReflectionClass($this);
         // 获取类的短名,就是不含命名空间(namespace)的那一部分
         return $reflector->getShortName();
     }

     /**
      * Returns the list of attribute names.
      * 返回属性名的列表
      *
      * By default, this method returns all public non-static properties of the class.
      * You may override this method to change the default behavior.
      * 默认只会返回 public 且不是 static 的属性,可以重写该方法定义不同的返回类型
      *
      * @return array list of attribute names.
      */
     public function attributes()
     {
         $class = new ReflectionClass($this);
         $names = [];
         /**
          *ReflectionClass::getProperties — ReflectionClass反射类的方法,用于获取一组属性,有下列参数:
           ReflectionProperty::IS_STATIC 表示返回 static 的属性。
           ReflectionProperty::IS_PUBLIC 表示返回 public 的属性。
           ReflectionProperty::IS_PROTECTED 表示返回 protected 的属性。
           ReflectionProperty::IS_PRIVATE 表示返回 private 的属性。
          */
         foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
             // 如果是public的属性,并且不是static的,就认为是它的attribute
             if (!$property->isStatic()) {
                 // 获取该属性的名称
                 $names[] = $property->getName();
             }
         }

         return $names;
     }

     /**
      * Returns the attribute labels.
      * 返回属性的标签
      *
      * Attribute labels are mainly used for display purpose. For example, given an attribute
      * `firstName`, we can declare a label `First Name` which is more user-friendly and can
      * be displayed to end users.
      * 属性标签主要用于显示目的,例如给属性'firstName'一个‘First Name’标签,能够更友好的显示给最终用户
      *
      * By default an attribute label is generated using [[generateAttributeLabel()]].
      * This method allows you to explicitly specify attribute labels.
      * 默认调用[[generateAttributeLabel()]]方法生成标签
      * Note, in order to inherit labels defined in the parent class, a child class needs to
      * merge the parent labels with child labels using functions such as `array_merge()`.
      * 注意,为了继承父类的标签,之类需要调用`array_merge()`合并父类的标签
      *
      * @return array attribute labels (name => label)
      * @see generateAttributeLabel()
      */
     public function attributeLabels()
     {
         return [];
     }

Yii源码阅读笔记(十四)的更多相关文章

  1. Yii源码阅读笔记(四)

    所有控制器action的基类yii\base\Action.php namespace yii\base;//定义的命名空间 use Yii //使用的命名空间 class Action extend ...

  2. Mina源码阅读笔记(四)—Mina的连接IoConnector2

    接着Mina源码阅读笔记(四)-Mina的连接IoConnector1,,我们继续: AbstractIoAcceptor: 001 package org.apache.mina.core.rewr ...

  3. Yii源码阅读笔记(一)

    今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...

  4. Vue2.0源码阅读笔记(四):nextTick

      在阅读 nextTick 的源码之前,要先弄明白 JS 执行环境运行机制,介绍 JS 执行环境的事件循环机制的文章很多,大部分都阐述的比较笼统,甚至有些文章说的是错误的,以下为个人理解,如有错误, ...

  5. Mina源码阅读笔记(四)—Mina的连接IoConnector1

    上一篇写的是IoAcceptor是服务器端的接收代码,今天要写的是IoConnector,是客户端的连接器.在昨天,我们还留下一些问题没有解决,这些问题今天同样会产生,但是都要等到讲到session的 ...

  6. Yii源码阅读笔记(三十四)

    Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...

  7. Yii源码阅读笔记(二十四)

    Module类中获取子模块,注册子模块,实例化控制器,根据路由运行指定控制器方法的注释: /** * Retrieves the child module of the specified ID. * ...

  8. Yii源码阅读笔记(三十五)

    Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...

  9. Yii源码阅读笔记(三十二)

    web/Application类的注释,继承base/Application类,针对web应用的一些处理: namespace yii\web; use Yii; use yii\base\Inval ...

随机推荐

  1. 我的MySQL5.6免安装版配置过程

    最近打算学习MySQL,第一步就是安装.下载到一个面安装版.解压到我的D盘的mysql目录. 弄了一个最简单的配置文件.目录中只有一个my-default.ini,基本没啥用.在网上弄了一个my.in ...

  2. C#正则表达式分组使用

    string s = "dfasfdasfdsa*fdasfdsafdsa"; s = "转发:dfasfdasfdsa*fdasfdsafdsa"; //s ...

  3. 利用phpexcel把excel导入数据库和数据库导出excel实现

    <?php ); ini_set(,,,date(,date(,,,date(,,,date(,date(,,,date()     ->setCellValue();); $objPHP ...

  4. Struts表单格局;theme三个属性值:simple,xhtml,css_xhtml

    转自:http://www.educity.cn/wenda/7156.html 解决Struts2 Form表单自己布局之前先看看 theme 属性, theme属性提供 三个属性值:simple, ...

  5. BZOJ 1072: [SCOI2007]排列perm 状态压缩DP

    1072: [SCOI2007]排列perm Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为 ...

  6. Java配置环境变量、方法和原因

    首先,你应该已经安装了 java 的 JDK 了,笔者安装的是:jdk-7u7-windows-x64 接下来主要讲怎么配置 java 的环境变量,也是为了以后哪天自己忘记了做个备份 1.进入“计算机 ...

  7. Spotlight on MySQL监控MySQL服务器

    第一步: 下载并安装mysql-connector-3.5x Spotlight on MySQL 连接mysql必须使用mysql-connector-3.5x,5.3.2版本我试了下不行,有兴趣可 ...

  8. 关于C语言中for循环的执行顺序

    for(初始值赋值操作A:终止条件B:递增操作C) {      循环体D: } 其执行次序为:A->B->D->C->B->D->C->B--.. 直到B条 ...

  9. Xamarin.iOS编译时无法连接苹果系统

    Xamarin.iOS编译时无法连接苹果系统   错误信息:Unable to connect to Address=’***.***.***.***’ with User=’***’   即使Vis ...

  10. Xamarin.Forms项目无法添加服务引用

    Xamarin.Forms项目无法添加服务引用 创建的Xamarin.Forms项目中,右击“引用”选项,在弹出的菜单中没有“添加服务引用”命令.这是由于该项目是支持Windows Phone 8.1 ...