Yii2 Framework Essay 15

Source: Internet
Author: User

System to look at model.php

<?PHP/** * @linkhttp://www.yiiframework.com/* @copyright Copyright (c) Yii software LLC * @licensehttp://www.yiiframework.com/license/ */namespaceYii\Base; use yii;use arrayaccess;use arrayobject;use arrayiterator;use reflectionclass;use iteratoraggregate;use yii\ Helpers\inflector;use Yii\validators\requiredvalidator;use Yii\validators\validator;/*  * * Model is the base class for data models. * Model is the base class * Iteratoraggregate (Aggregation iterator) interface for all models-an interface for creating an external iterator that implements the Getiterator method. * iteratoraggregate::getiterator-gets an external iterator that foreach calls the method. * * Arrayaccess (array access) interface-provides an interface for accessing an object like an array, as follows: * arrayaccess::offsetexists-check for an offset position exists * Arrayaccess::offset Get-gets the value of an offset position * arrayaccess::offsetset-Sets the value of an offset position * arrayaccess::offsetunset-Resets the value of an offset position * used in Model to implement will $model [$fi ELD] Replace with $model $field * * Model implements the following commonly used features: * The model implements the following commonly used features: *-Attribute Declar Ation:by default, every public class member is considered as * a model attribute * attribute declaration: By default, each common class member is considered to be a model property. *-Attribute Labels:each attribute may is associated with a label for display purpose * Property Label: Each property might be associated with the purpose of a label display *-Mas sive attribute Assignment * Large-scale attribute assignment *-scenario-based validation * scenario-based validation * Model also raises the following events when Performing Data validation: * The following events are also triggered when the model performs the validation *-[[event_before_validate]]: An event for raised at the beginning of [[[Validate ()]] *-A events before data validation *-[[[Event_after_validate]]: An event raised at the EN D of [[[Validate ()]] * An event after data validation * directly use the model to the store model data, or extend it with customization. * You can store the data model directly using model, or you can customize it yourself. * @property \yii\validators\validator[] $activeValidators the validators applicable to the current * [[scenario]]. This property is read-only. This property is read-only. * @property array $attributes Attribute VALUES (name = = value). * @property Array $errors An array of errors to all attributes. Empty array is returned if no error. The * result is a two-dimensional array. see [[GetErrors ()]] for detailed description. This property is read-only. * Error Array returns error information for all properties, no error when array is empty. is also read-only. * @property Array $firstErrors the first errors. The array keys are the attribute names, and the array values * is the corresponding error messages. An empty array would be returned if there is no error. This property is * read-only. * The first error in the array, the key for the array is the property name, the array'sThe value is the appropriate error message. If there are no errors, an empty array is returned. is also read-only. * @property Arrayiterator $iterator A iterator for traversing the items in the list. This property is * read-only. * Iterator properties, iterating through the items in the list, and the properties are read-only. * @property string $scenario the scenario. Defaults to [[Scenario_default]]. * This model of the scene, the default is [[Scenario_default]]. * @property arrayobject|\yii\validators\validator[] $validators all the validators declared in the model. * Validators property, all validator models in the model. * This property is read-only.  * Read Only * @author Qiang Xue <[email protected]> * @since 2.0*/classModel extends Component implements Iteratoraggregate, arrayaccess, arrayable{use arrayabletrait;//using arrayabletrait, similar to inheriting a arrayable class    /** * The name of the default scenario. * Name of default scene*/    ConstScenario_default ='default'; /** * @event modelevent an event raised at the beginning of [[Validate ()]].     set * [[[Modelevent::isvalid]] to is false to stop the validation. * [[[Validate ()]] event constants triggered before start, deactivated by setting [[Modelevent::isvalid]]*/    ConstEvent_before_validate ='beforevalidate'; /** * @event Event an event raised at the end of [[Validate ()]] * Events constants are triggered after [[Validate ()]]. */    ConstEvent_after_validate ='aftervalidate'; /** * @var Array validation errors (attribute name + array of errors) * Verified error message*/    Private$_errors; /** * @var arrayobject List of validators * Arrayobject Validator lists*/    Private$_validators; /** * @var String Current scenario * is the default*/    Private$_scenario =Self::scenario_default; /** * Returns the validation rules for attributes.     * * Return Property validation rules * * Validation rules is used by [[Validate ()]] to check if attribute values is valid.     * Child classes could override this method to declare different validation rules.     * If the attribute value is valid, the validation rule is checked using [[Validate ()]).     * Subclasses can override this method to declare different validation rules. * Each of the rule is a array with the following structure: * format as follows: * * ~ ~ ~ * [* [' attribute1 ', ' attrib Ute2 '],//attribute list * ' validator type ',//Verify the type * ' on ' = = [' Scenario1 ', ' Scenario2 '],//application scene * ... ot Her parameters ...//other parameters *] * ~ ~ ~ * WHERE * *-attribute list:required, specifies the Attri     Butes array to is validated, for single attribute can pass string; * Attribute Name list: Required, specify the property name to validate *-Validator type:required, specifies the validator to be used. It can be a built-in validator name, * Authenticator type: Required, specifies the authenticator to use. It can be the name of a built-in validator, * A method name of the model class, an anonymous Function, or a validator class name.     * The method name of the model class, an anonymous function, or the name of a confirmation class.  *-On:optional, specifies the [[Scenario|scenarios]] array when the validation * Specifies optional array validation parameters * rule can be Applied.     If This option was not set, the rule would apply to all scenarios.     * If this option is not set, the rule will apply to all cases.     *-Additional Name-value pairs can be specified to initialize the corresponding validator properties.     * Refer to Individual validator class API for possible properties. * * A validator can be either an object of A class extending [[Validator]], or a model class method * (called *INL INE validator*) that have the following signature: * Validator can be an instance of [[validator]] or a method of model mode * ~ ~ ~ ~ ~//$     The params refers to validation parameters given in the rule * validates the rules given by the parameter. * Function Validatorname ($attribute, $params) * ~ ~ * In the above ' $attribute ' refers to currently validate d attribute name while ' $params ' contains an array of * Validator configuration options such as ' Max ' in case of ' string ' validator.     Currently validate attribute value * can be accessed as ' $this->[$attribute] '.     * * Yii also provides a set of [[Validator::builtinvalidators|built-in validators]].     * They each have an alias name which can is used when specifying a validation rule.     * Each of them has an alias when the specified validation rule can be used. * Below is some examples: * * ~ ~ * [*//built-in "required" validator * [[' username ', ' pas Sword '], ' required '], *//built-in "string" Validator customized with "Min" and "Max" Properties * [' User Name ', ' String ', ' min ' = 3, ' max ' = + ', *//built-in "compare" validator that's used in "register" Scen Ario only * [' password ', ' compare ', ' compareattribute ' = ' password2 ', ' on ' = ' register '], *//an Inline validator defined via the "Authenticate ()" method in the Model class * [' Password ', ' Authenticate ', ' on ' =& Gt ' Login '], *//a Validator of Class "Daterangevalidator" * [' daterange ', ' daterangevalidator '], *]; * ~ ~ ~ * Note, in order to inherit rules defined in the parent class, a child class needs to * merge the Pare     NT rules with the child rules using functions such as ' Array_merge () '.      * If you want to inherit the validation rule from the parent class, you need to merge the validation rule array through the Array_merge () function * @return Array validation rules * @see Scenarios ()*/     Publicfunction Rules () {return []; }    /** * Returns A list of scenarios and the corresponding active attributes.     * An active attribute are one that is subject to validation in the current scenario. * Returns a list of the scene and its corresponding active properties * The returned array should is in the following format: * * ~ ~ ~ * [* ' s     Cenario1 ' = = [' Attribute11 ', ' attribute12 ', ...], * ' scenario2 ' + = [' Attribute21 ', ' attribute22 ', ...],     * ... * * * ~ ~ ~ * By default, an active attribute is considered safe and can be massively assigned.     * By default, an active attribute is considered safe and can be distributed on a large scale. * If an attribute should not being massively assigned (thus considered unsafe), * if a property should not be allocated on a large scale (this would be considered unsafe), * pleas     e prefix the attribute with an exclamation character (e.g. '!rank ').     * Please add a prefix attribute (e.g. '!rank ') before the exclamation character.     * The default implementation of this method would return all scenarios found in the [[Rules ()]] * declaration.     * The default implementation of this method will return the declaration of the [Rules ()] method found in all scenarios. * A Special scenario named [[SCEnario_default]] would contain all attributes * found in the [[Rules ()]].     * A special scene called [[[Scenario_default]] will contain all attributes * in [[rules]].      * * @return Array A list of scenarios and the corresponding active attributes.*/     Publicfunction Scenarios () {//Default Scenario$scenarios = [Self::scenario_default] []]; foreach($ This->getvalidators () as$validator) {            //Loop Validator, take out all the mentioned scenes, including on and except            foreach($validator->on as$scenario) {$scenarios [$scenario]= []; }            foreach($validator->except as$scenario) {$scenarios [$scenario]= []; }        }        //Remove the name of all scenes$names =Array_keys ($scenarios); foreach($ This->getvalidators () as$validator) {            if(Empty ($validator->on) && Empty ($validatorexcept)) {                //If validator is not defined on, and no except is defined, it is placed in all the scenes                foreach($names as$name) {                    //All properties of the loop $validator                    foreach($validator->attributes as$attribute) {$scenarios [$name] [$attribute]=true; }}} elseif (Empty ($validator-On )) {                //if no on is defined                foreach($names as$name) {                    if(!in_array ($name, $validator->except,true)) {                        //and the scene is not in except, this attribute is added to the corresponding scene                        foreach($validator->attributes as$attribute) {$scenarios [$name] [$attribute]=true; }                    }                }            } Else {                //If you define the on                foreach($validator->on as$name) {                    //This attribute is added to the on-defined scene                    foreach($validator->attributes as$attribute) {$scenarios [$name] [$attribute]=true; }                }            }        }

Yii2 Framework Essay 15

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.