Example #1
0
 /**
  * rewrite set attributes, set relation data to relations, auto save at after save
  * @param array $values
  * @param bool $safeOnly
  * @author Lujie.Zhou(gao_lujie@live.cn, qq:821293064).
  */
 public function setAttributes($values, $safeOnly = true)
 {
     if (!is_array($values)) {
         return;
     }
     foreach ($values as $name => $value) {
         if (is_array($value) && isset($this->getMetaData()->relations[$name])) {
             $this->{$name} = $value;
         }
     }
     return parent::setAttributes($values, $safeOnly);
 }
 /**
  * Transfers collected values to the {@link HUserInfoForm::model}
  * 
  * @access public
  * @return void
  */
 public function getValidUserModel()
 {
     if ($this->hasErrors()) {
         return null;
     }
     // syncing only when we have a new model
     if ($this->_model->isNewRecord && strpos($this->scenario, '_pass') === false) {
         $this->_model->setAttributes(array($this->emailAtt => $this->email, $this->nameAtt => $this->username), false);
         if (HOAuthAction::$useYiiUser) {
             $this->_model->superuser = 0;
             $this->_model->status = Yii::app()->getModule('user')->activeAfterRegister ? User::STATUS_ACTIVE : User::STATUS_NOACTIVE;
             $this->_model->activkey = UserModule::encrypting(microtime() . $this->_model->email);
         }
     }
     return $this->model;
 }
Example #3
0
        /**
         * Extends setAttributes to handle active date fields
         *
         * @param $values array
         * @param $safeOnly boolean
         */
        public function setAttributes($values,$safeOnly=true)
        {
			foreach ($this->widgetAttributes() as $fieldName=>$className) {
				if (isset($values[$fieldName])&&class_exists($className)) {
					$class = new $className;
					$arr = $this->widgetParams($fieldName);
					if ($arr) {
						$newParams = $class->params;
						$arr = (array)CJavaScript::jsonDecode($arr);
						foreach ($arr as $p=>$v) {
							if (isset($newParams[$p])) $newParams[$p] = $v;
						}
						$class->params = $newParams;
					}
					if (method_exists($class,'setAttributes')) {
						$values[$fieldName] = $class->setAttributes($values[$fieldName],$this,$fieldName); 
					}
				}
			}
			parent::setAttributes($values,$safeOnly);
		}
Example #4
0
 public function setAttributes($values, $safeOnly = false, $withRelation = true)
 {
     $cols = $this->tableSchema->columns;
     foreach ($values as $k => $v) {
         if (isset($cols[$k])) {
             if ($cols[$k]->dbType == 'datetime' || $cols[$k]->dbType == 'date' || $cols[$k]->dbType == 'time') {
                 if ($values[$k] == '') {
                     $values[$k] = null;
                 } else {
                     $values[$k] = $values[$k];
                 }
             }
         }
     }
     parent::setAttributes($values, $safeOnly);
     if ($withRelation) {
         $this->initRelation();
         foreach ($this->__relations as $k => $r) {
             if ($k != 'currentModel' && isset($values[$k])) {
                 $rel = $this->getMetaData()->relations[$k];
                 $this->__relations[$k] = $values[$k];
                 if (is_string($values[$k]) || is_array($values[$k])) {
                     if (is_string($values[$k])) {
                         $attr = json_decode($values[$k], true);
                         if (!is_array($attr)) {
                             $attr = [];
                         }
                     } else {
                         $attr = $values[$k];
                     }
                     if (Helper::is_assoc($values[$k])) {
                         switch (get_class($rel)) {
                             case 'CHasOneRelation':
                             case 'CBelongsToRelation':
                                 foreach ($attr as $i => $j) {
                                     if (is_array($j)) {
                                         unset($attr[$i]);
                                     }
                                 }
                                 $relArr = $this->{$k};
                                 if (is_object($relArr)) {
                                     $relArr->setAttributes($attr, false, false);
                                 }
                                 $this->__relations[$k] = $attr;
                                 break;
                         }
                     }
                 }
             }
             if (isset($values[$k . 'Insert'])) {
                 $value = $values[$k . 'Insert'];
                 $value = is_string($value) ? json_decode($value, true) : $value;
                 $this->__relInsert[$k] = $value;
             }
             if (isset($values[$k . 'Update'])) {
                 $value = $values[$k . 'Update'];
                 $value = is_string($value) ? json_decode($value, true) : $value;
                 $this->__relUpdate[$k] = $value;
             }
             if (isset($values[$k . 'Delete'])) {
                 $value = $values[$k . 'Delete'];
                 $value = is_string($value) ? json_decode($value, true) : $value;
                 $this->__relDelete[$k] = $value;
             }
             $this->applyRelChange($k);
         }
     }
     $ap = $this->getAttributeProperties();
     foreach ($ap as $k => $r) {
         if (isset($values[$k])) {
             $this->{$k} = $values[$k];
         }
     }
 }
Example #5
0
 public function setAttributes($values, $safeOnly = true)
 {
     if (isset($values['type']) && $this->type !== $values['type']) {
         $this->_typeChanged = true;
     }
     return parent::setAttributes($values, $safeOnly);
 }
Example #6
0
 public function setAttributes($values, $safeOnly = true)
 {
     return parent::setAttributes(DataHelper::arrayKeysCamelToSnake($values), $safeOnly);
 }
Example #7
0
 public function setAttributes($values, $safeOnly = true)
 {
     if (count($this->getFields()) > 0) {
         $test = __CLASS__ . ".setAttributes:\n";
         foreach ($values as $k => $v) {
             $test .= "[{$k}={$v}]\n";
         }
         $test .= "\nparse field values:\n";
         foreach ($values as $fieldName => $value) {
             $test .= "{$fieldName}...";
             $boolFound = false;
             foreach ($this->getFields() as $f) {
                 if ($f->fieldname == $fieldName) {
                     $test .= " found. setfieldvalue:[{$value}]\n";
                     $f->setFieldValue($value);
                     $boolFound = true;
                     break;
                 }
             }
             if ($boolFound == false) {
                 $test .= " [not found]\n";
             }
         }
         Yii::log($test, "info");
     }
     parent::setAttributes($values);
 }
 /**
  * Sets the attribute and flags values in a massive way.
  * @param array $values attribute values (name=>value) to be set.
  * @param boolean $safeOnly whether the assignments should only be done to the safe attributes.
  * A safe attribute is one that is associated with a validation rule in the current {@link scenario}.
  * @see getSafeAttributeNames
  * @see attributeNames
  */
 public function setAttributes($values, $safeOnly = true)
 {
     $flags = $this->cachedFlags();
     $notFlags = array();
     foreach ($values as $key => $val) {
         if (array_key_exists($key, $flags)) {
             $this->setFlag($key, $val);
         } else {
             $notFlags[$key] = $val;
         }
     }
     parent::setAttributes($notFlags, $safeOnly);
 }
Example #9
0
 public function setAttributes($values, $safeOnly = null)
 {
     if ($safeOnly === null) {
         $safeOnly = $this->setOnlySafeAttributes;
     }
     return parent::setAttributes($values, $safeOnly);
 }
 /**
  * Copies attributes from one active record to another, skips primary key.
  * 
  * @param CActiveRecord $from                record to copy from
  * @param CActiveRecord $to                  record to copy to
  * @param array         $skipAttributes      attribute names which should not be copied
  * @param array         $forceCopyAttributes attribute names which should be force copied
  */
 protected static function copyAttributes($from, $to, $skipAttributes = array(), $forceCopyAttributes = array())
 {
     $attributes = $from->getAttributes();
     $pk = $to->getMetaData()->tableSchema->primaryKey;
     if (!is_array($pk)) {
         $skipAttributes[] = $pk;
     } else {
         $skipAttributes = array_merge($skipAttributes, $pk);
     }
     $skipAttributes = array_diff($skipAttributes, $forceCopyAttributes);
     foreach ($skipAttributes as $attribute) {
         unset($attributes[$attribute]);
     }
     $to->setAttributes($attributes, true);
 }