Connection类getAttribute方法 改成 getConfig 增加setConfig方法 Model类类型转换支持格式定义

This commit is contained in:
thinkphp
2016-04-23 11:17:53 +08:00
parent 138dab1e9e
commit 2fb064cb37
5 changed files with 35 additions and 11 deletions

View File

@@ -839,13 +839,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = $this->$method($value, $this->data);
} elseif (isset($this->type[$name])) {
// 类型转换
$type = $this->type[$name];
$type = $this->type[$name];
list($type, $param) = explode(':', $type);
switch ($type) {
case 'integer':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
if (empty($param)) {
$value = (float) $value;
} else {
$value = (float) number_format($value, $param);
}
break;
case 'boolean':
$value = (bool) $value;
@@ -858,6 +863,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = json_encode($value, JSON_FORCE_OBJECT);
}
break;
case 'json':
case 'array':
if (is_array($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
@@ -891,20 +897,27 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $this->$method($value, $this->data);
} elseif (!is_null($value) && isset($this->type[$name])) {
// 类型转换
$type = $this->type[$name];
$type = $this->type[$name];
list($type, $param) = explode(':', $type);
switch ($type) {
case 'integer':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
if (empty($param)) {
$value = (float) $value;
} else {
$value = (float) number_format($value, $param);
}
break;
case 'boolean':
$value = (bool) $value;
break;
case 'datetime':
$value = date($this->dateFormat, $value);
$format = $param ?: $this->dateFormat;
$value = date($format, $value);
break;
case 'json':
case 'array':
$value = json_decode($value, true);
break;