Vendor/yiisoft/yii2/base/model.php (continued)
/** * Set attribute value * @param array $values attribute values (name = value) to being assigned to the model. * @param Boolean $safeOnly whether the assignments should only is done to the safe attributes. * A security attribute is associated with a validation rule in the current [[case]]. * @see safeattributes () * @see attributes ()*/ Public functionSetAttributes ($values,$safeOnly=true) { //must be a number of groups if(Is_array($values)) { //array_flip-Exchange The keys and values in the array//put the attributes on the key//the properties in the default fetch safeattributes $attributes=Array_flip($safeOnly?$this->safeattributes ():$this-attributes ()); foreach($values as $name=$value) { if(isset($attributes[$name])) { //If the attribute is present, the value is directly assigned $this-$name=$value; } ElseIf($safeOnly) { //If it doesn't exist, and it's safeonly, trigger the Onunsafeattribute method. $this->onunsafeattribute ($name,$value); } } } } /** * This method was invoked when a unsafe attribute is being massively assigned. * The default implementation would log a warning message if Yii_debug is on. * It does nothing otherwise. * @param string $name The unsafe attribute name * @param mixed $value the attribute value*/ Public functionOnunsafeattribute ($name,$value) { if(yii_debug) {//if it is a debug state, the unsafe properties that are not successfully set will be hit log recordYii::trace ("Failed to set unsafe attribute"$name' In '.Get_class($this) . "‘.",__method__); } } /** * Returns The scenario that this model was used in. * * Scenario affects how validation are performed and which attributes can * be massively assigned. * * @return String the scenario. Defaults to [[Scenario_default]]. */ Public functionGetscenario () {//get the current scene return $this-_scenario; } /** * Sets the scenario for the model. * Note that this method does not a check if the scenario exists or not. * the method [[Validate ()]] would perform this check. * @param string $value the scenario. */ Public functionSetscenario ($value) { //set the current scene $this->_scenario =$value; } /** * Returns the attribute names that is safe to being massively assigned in the current scenario. * @return string[] Safe attribute names*/ Public functionsafeattributes () {//get the current scene $scenario=$this-Getscenario (); //get all scenes and their properties $scenarios=$this-scenarios (); if(!isset($scenarios[$scenario])) { //The scene does not exist, it returns an empty return []; } $attributes= []; foreach($scenarios[$scenario] as $attribute) { //will start not! Attribute will be put into safeattributes, that is! Properties that begin with are not placed in Safeattributes if($attribute[0]!== '! ') { $attributes[] =$attribute; } } return $attributes; } /** * Returns the attribute names that is subject to validation on the current scenario. * @return string[] Safe attribute names*/ Public functionactiveattributes () {//Ibid . $scenario=$this-Getscenario (); $scenarios=$this-scenarios (); if(!isset($scenarios[$scenario])) { return []; } //get all the properties in the current scene $attributes=$scenarios[$scenario]; foreach($attributes as $i=$attribute) { //If the property name is! Start, just put it! Intercept//! The property begins with the rules, Plus! The ability to make rules (that is, validator) effective, but not to appear in Safeattributes if($attribute[0] = = = '! ') { $attributes[$i] =substr($attribute, 1); } } return $attributes; } /** * Populates the model with the data from end user. * The data to being loaded is ' $data [FormName] ', where ' formName ' refers to the value of [[FormName ()]]. * If [[FormName ()]] is empty, the whole ' $data ' array would be used to populate the model. * The data being populated is subject to the safety check by [[SetAttributes ()]]. * Load the data into the model where * @param array $data the data array. This was usually ' $_post ' or ' $_get ', but can also was any valid array * supplied by end user. * @param string $formName The form name to being used for loading the data into the model. * If not set, [[FormName ()]] would be used. * @return Boolean whether the model is successfully populated with some data. */ Public functionLoad$data,$formName=NULL) { //If there is a yii form, use the form, or you will get the name of the class (without namespace) $scope=$formName===NULL?$this->formname ():$formName; if($scope= = = ' &&!Empty($data)) { //If the $scope is an empty string and the $data is not empty, the property is set//is $formName an empty string, and $data is not empty $this->setattributes ($data); return true; } ElseIf(isset($data[$scope])) { //Otherwise, there must be $data [$scope], using $data [$scope] To set the property $this->setattributes ($data[$scope]); return true; } Else { return false; } } /** * Populates a set of models with the data from end user. * Load the data into the collection of the model where * This method is mainly used to collect tabular data input. * The data to was loaded for each model was ' $data [Formname][index] ', where ' formName ' * refers to the value of [[Formna Me ()]], and ' index ' the index of the model in the ' $models ' array. * If [[FormName ()]] is empty, ' $data [index] ' would be used to populate each model. * The data being populated to each model are subject to the safety check by [[SetAttributes ()]]. * @param array $models the models to be populated. Note that all models should has the same class. * @param array $data the data array. This was usually ' $_post ' or ' $_get ', but can also was any valid array * supplied by end user. * @param string $formName The form name to being used for loading the data into the models. * If not set, it would use the [[[FormName ()]] value of the first model in ' $models '. * This parameter is available since VersiOn 2.0.1. * @return Boolean whether at least one of the models is successfully populated.*/ Public Static functionLoadmultiple ($models,$data,$formName=NULL) { if($formName===NULL) { /*@var $first Model*/ //reset-The inner pointer of an array to the first cell $first=Reset($models); if($first===false) { //return False if not present return false; } //get the name of the class (excluding namespace) $formName=$first-FormName (); } $success=false; //traverse $models, load data foreach($models as $i=$model) { /*@var $model Model*/ if($formName= = "') { if(!Empty($data[$i])) { //data is not empty, load into the corresponding model $model->load ($data[$i], ‘‘); $success=true; } } ElseIf(!Empty($data[$formName][$i])) { //$formName is present and the data is not empty, load into the corresponding model $model->load ($data[$formName][$i], ‘‘); $success=true; } } return $success; }
)
Learning yii2.0 Framework Read code (eight)