本文整理汇总了PHP中CModel::setAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP CModel::setAttributes方法的具体用法?PHP CModel::setAttributes怎么用?PHP CModel::setAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CModel
的用法示例。
在下文中一共展示了CModel::setAttributes方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setAttributes
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);
}
示例2: setAttributes
/**
* 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);
}
示例3: setAttributes
/**
* Автоматическая установка всех полей в записи из запроса
*
* @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);
}
}
}
示例4: setAttributes
/**
* 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);
}