Model增加类型自动转换设置属性 type, Driver类取消强制自动类型转换

This commit is contained in:
thinkphp
2016-04-07 16:53:29 +08:00
parent fb4f63266c
commit 28fbc5fde3
2 changed files with 56 additions and 23 deletions

View File

@@ -38,7 +38,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $cache = [];
// 记录改变字段
protected $change = [];
// 数据表主键
// 数据表主键 复合主键使用数组定义
protected $pk = 'id';
// 错误信息
protected $error;
@@ -57,6 +57,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
'update_time' => 'time',
];
// 字段类型或者格式转换
protected $type = [];
// 当前执行的关联类型
private $relation;
// 是否为更新
@@ -154,6 +156,26 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = $this->$method($value, $this->data);
}
// 类型转换 或者 字符串处理
if (isset($this->type[$name])) {
$type = $this->type[$name];
switch ($type) {
case 'integer':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
break;
case 'boolean':
$value = (bool) $value;
break;
default:
if (is_callable($type)) {
$value = call_user_func_array($type, [$value]);
}
}
}
// 设置数据对象属性
if (isset($this->data[$name]) && $this->data[$name] != $value && !in_array($name, $this->change)) {
$this->change[] = $name;
@@ -172,6 +194,26 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{
$value = isset($this->data[$name]) ? $this->data[$name] : null;
// 类型转换
if (!is_null($value) && isset($this->type[$name])) {
$type = $this->type[$name];
switch ($type) {
case 'integer':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
break;
case 'boolean':
$value = (bool) $value;
break;
default:
if (is_callable($type)) {
$value = call_user_func_array($type, [$value]);
}
}
}
// 检测属性获取器
$method = 'get' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) {