public function setAttributes($values, $safeOnly = false) { foreach ($values as $k => $v) { if (!in_array($k, $this->attributeNames())) { // this exception is thrown when an invalid attribute is specified // in the EYuiForm widget array fielddefs or pages. throw new Exception("invalid attribute name: " . $k . ", value=" . $v); } } parent::setAttributes($values, false); }
/** * Override of setAttributes in CModel to support setting attributes into this form as well * as the related model. Splits $values into two arrays. First array is name/value pairs of attributes * on this form, whereas the second array is name/value pairs on the model. */ public function setAttributes($values, $safeOnly = true) { $formValues = array(); $modelValues = array(); foreach ($values as $name => $value) { if (property_exists($this, $name)) { $formValues[$name] = $value; } else { $modelValues[$name] = $value; } } parent::setAttributes($formValues, $safeOnly); $this->model->setAttributes($modelValues, $safeOnly); }
/** * Автоматическая установка всех полей в записи из запроса * * @param array $array */ public function setAttributes(array $array) { parent::setAttributes($array); // дополнительные обработчики foreach ($this->fieldsProperty() as $field => $property) { if ($property['type'] == FIELD_MYSQL_DATE) { // преобразование даты из отечественного формата в mysql-формат $format = "Y-m-d H:i:s"; if (array_key_exists("mysql_format", $property)) { $format = $property["mysql_format"]; } if (array_key_exists($field, $array)) { if ($array[$field] !== "") { if (strtotime($array[$field]) !== false) { $value = date($format, strtotime($array[$field])); $array[$field] = $value; } } else { $array[$field] = date($format, 0); } } } } // поля многие-ко-многим тоже, почему бы и нет foreach ($this->relations() as $field => $relation) { if ($relation['relationPower'] == RELATION_MANY_TO_MANY) { if (array_key_exists($field, $array)) { $values = $array[$field]; $useManager = true; if (array_key_exists("targetClass", $relation)) { $useManager = false; } else { $manager = $relation['managerClass']; $getter = $relation['managerGetObject']; } // разрешим не указывать if (!array_key_exists("storageProperty", $relation)) { $relation["storageProperty"] = "_" . $field; } $property = $relation['storageProperty']; $this->{$property} = new CArrayList(); foreach ($values as $value) { if ($useManager) { $related = $manager::$getter($value); } else { $method = "get" . mb_substr($relation["targetClass"], 1); $related = CBaseManager::$method($value); } if (!is_null($related)) { $this->{$property}->add($related->getId(), $related); } } } } } // поля из базы foreach ($array as $key => $value) { if ($this->getDbTable()->getFields()->hasElement($key)) { $this->getRecord()->setItemValue($key, $value); } } }
/** * Overridden to skip placeholder values */ public function setAttributes($values, $safeOnly = true) { if (!is_array($values)) { return; } $protectedFields = array_flip($this->getProtectedFields()); foreach ($values as $fieldName => $value) { if (isset($protectedFields[$fieldName]) && $value === $this->getProtectedFieldPlaceholder()) { unset($values[$fieldName]); } } return parent::setAttributes($values, $safeOnly); }