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

@@ -13,7 +13,7 @@
define('START_TIME', microtime(true)); define('START_TIME', microtime(true));
define('START_MEM', memory_get_usage()); define('START_MEM', memory_get_usage());
// 版本信息 // 版本信息
define('THINK_VERSION', '5.0.0 RC2'); define('THINK_VERSION', '5.0.0 RC3');
// 系统常量 // 系统常量
defined('DS') or define('DS', DIRECTORY_SEPARATOR); defined('DS') or define('DS', DIRECTORY_SEPARATOR);
defined('THINK_PATH') or define('THINK_PATH', dirname(__FILE__) . DS); defined('THINK_PATH') or define('THINK_PATH', dirname(__FILE__) . DS);

View File

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

View File

@@ -89,7 +89,7 @@ abstract class Builder
$result = []; $result = [];
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
if (!in_array($key, $fields, true)) { if (!in_array($key, $fields, true)) {
if ($this->connection->getAttribute('fields_strict')) { if ($this->connection->getConfig('fields_strict')) {
throw new Exception(' fields not exists :[' . $key . ']'); throw new Exception(' fields not exists :[' . $key . ']');
} }
} else { } else {
@@ -106,7 +106,6 @@ abstract class Builder
$this->query->bind($key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR); $this->query->bind($key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
$result[$item] = ':' . $key; $result[$item] = ':' . $key;
} }
} }
} }
} }
@@ -573,7 +572,7 @@ abstract class Builder
foreach ($dataSet as &$data) { foreach ($dataSet as &$data) {
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
if (!in_array($key, $fields, true)) { if (!in_array($key, $fields, true)) {
if ($this->connection->getAttribute('fields_strict')) { if ($this->connection->getConfig('fields_strict')) {
throw new Exception(' fields not exists :[' . $key . ']'); throw new Exception(' fields not exists :[' . $key . ']');
} }
unset($data[$key]); unset($data[$key]);

View File

@@ -167,11 +167,23 @@ abstract class Connection
* @param string $config 配置名称 * @param string $config 配置名称
* @return mixed * @return mixed
*/ */
public function getAttribute($config) public function getConfig($config)
{ {
return $this->config[$config]; return $this->config[$config];
} }
/**
* 设置数据库的配置参数
* @access public
* @param string $config 配置名称
* @param mixed $value 配置值
* @return void
*/
public function setConfig($config, $value)
{
$this->config[$config] = $value;
}
/** /**
* 连接数据库方法 * 连接数据库方法
* @access public * @access public

View File

@@ -336,7 +336,7 @@ class Query
} }
} }
} else { } else {
$prefix = $this->connection->getAttribute('prefix'); $prefix = $this->connection->getConfig('prefix');
// 传入的表名为数组 // 传入的表名为数组
if (is_array($join)) { if (is_array($join)) {
if (0 !== $key = key($join)) { if (0 !== $key = key($join)) {