改进字段的修改器、获取器和自动类型转换的优先级 并且不再同时执行

This commit is contained in:
thinkphp
2016-04-09 15:11:29 +08:00
parent 5a70087133
commit f68f27e245

View File

@@ -54,8 +54,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $insert = []; protected $insert = [];
// 更新自动完成列表 // 更新自动完成列表
protected $update = []; protected $update = [];
// 时间戳字段列表 // 自动写入的时间戳字段列表
protected $timestampField = ['create_time', 'update_time']; protected $autoTimeField = ['create_time', 'update_time', 'delete_time'];
// 时间字段取出后的时间格式
protected $dateFormat = 'Y-m-d H:i:s';
// 字段类型或者格式转换 // 字段类型或者格式转换
protected $type = []; protected $type = [];
@@ -173,18 +175,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
if (is_null($value) && in_array($name, $this->timestampField)) { if (is_null($value) && in_array($name, $this->autoTimeField)) {
// 如果是时间戳字段 则自动写入 // 自动写入的时间戳字段
$value = NOW_TIME; $value = NOW_TIME;
} else { } else {
// 检测修改器 // 检测修改器
$method = 'set' . Loader::parseName($name, 1) . 'Attr'; $method = 'set' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$value = $this->$method($value, $this->data); $value = $this->$method($value, $this->data);
} } elseif (isset($this->type[$name])) {
// 类型转换
// 类型转换 或者 字符串处理
if (isset($this->type[$name])) {
$type = $this->type[$name]; $type = $this->type[$name];
switch ($type) { switch ($type) {
case 'integer': case 'integer':
@@ -196,6 +196,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'boolean': case 'boolean':
$value = (bool) $value; $value = (bool) $value;
break; break;
case 'datetime':
$value = strtotime($value);
break;
case 'object':
if (is_object($value)) {
$value = json_encode($value, JSON_FORCE_OBJECT);
}
break;
case 'array': case 'array':
if (is_array($value)) { if (is_array($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE); $value = json_encode($value, JSON_UNESCAPED_UNICODE);
@@ -223,8 +231,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{ {
$value = isset($this->data[$name]) ? $this->data[$name] : null; $value = isset($this->data[$name]) ? $this->data[$name] : null;
// 检测属性获取器
$method = 'get' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) {
return $this->$method($value, $this->data);
} elseif (!is_null($value) && isset($this->type[$name])) {
// 类型转换 // 类型转换
if (!is_null($value) && isset($this->type[$name])) {
$type = $this->type[$name]; $type = $this->type[$name];
switch ($type) { switch ($type) {
case 'integer': case 'integer':
@@ -236,19 +248,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'boolean': case 'boolean':
$value = (bool) $value; $value = (bool) $value;
break; break;
case 'datetime':
$value = date($this->dateFormat, $value);
break;
case 'array': case 'array':
$value = json_decode($value, true); $value = json_decode($value, true);
break; break;
case 'object':
$value = json_decode($value);
break;
} }
} } elseif (is_null($value) && method_exists($this, $name)) {
// 检测属性获取器
$method = 'get' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) {
return $this->$method($value, $this->data);
}
if (is_null($value) && method_exists($this, $name)) {
// 执行关联定义方法 // 执行关联定义方法
$db = $this->$name(); $db = $this->$name();
// 判断关联类型执行查询 // 判断关联类型执行查询
@@ -298,6 +308,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
unset($this->data[$name]); unset($this->data[$name]);
} }
/**
* 获取当前模型对象的主键
* @access public
* @param string $tableName 数据表名
* @return mixed
*/
public function getPk($tableName = '')
{
if ($this->pk) {
return $this->pk;
} else {
return self::db()->getTableInfo($tableName, 'pk');
}
}
/** /**
* 判断一个字段名是否为主键字段 * 判断一个字段名是否为主键字段
* @access public * @access public
@@ -306,7 +331,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
protected function isPk($key) protected function isPk($key)
{ {
$pk = $this->pk; $pk = $this->getPk();
if (is_string($pk) && $pk == $key) { if (is_string($pk) && $pk == $key) {
return true; return true;
} elseif (is_array($pk) && in_array($key, $pk)) { } elseif (is_array($pk) && in_array($key, $pk)) {