From 2fb064cb37051cb1dbbc4ef40d115182aeb53a02 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 23 Apr 2016 11:17:53 +0800 Subject: [PATCH] =?UTF-8?q?Connection=E7=B1=BBgetAttribute=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20=E6=94=B9=E6=88=90=20getConfig=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?setConfig=E6=96=B9=E6=B3=95=20Model=E7=B1=BB=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E6=94=AF=E6=8C=81=E6=A0=BC=E5=BC=8F=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 2 +- library/think/Model.php | 23 ++++++++++++++++++----- library/think/db/Builder.php | 5 ++--- library/think/db/Connection.php | 14 +++++++++++++- library/think/db/Query.php | 2 +- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/base.php b/base.php index db918e7f..a60c6988 100644 --- a/base.php +++ b/base.php @@ -13,7 +13,7 @@ define('START_TIME', microtime(true)); 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('THINK_PATH') or define('THINK_PATH', dirname(__FILE__) . DS); diff --git a/library/think/Model.php b/library/think/Model.php index bc390fd0..0b2bee0e 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -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; diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 905a6905..3d5be97a 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -89,7 +89,7 @@ abstract class Builder $result = []; foreach ($data as $key => $val) { 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 . ']'); } } else { @@ -106,7 +106,6 @@ abstract class Builder $this->query->bind($key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR); $result[$item] = ':' . $key; } - } } } @@ -573,7 +572,7 @@ abstract class Builder foreach ($dataSet as &$data) { foreach ($data as $key => $val) { 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 . ']'); } unset($data[$key]); diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index c16956e7..adfc1635 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -167,11 +167,23 @@ abstract class Connection * @param string $config 配置名称 * @return mixed */ - public function getAttribute($config) + public function getConfig($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 diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 7f6ca17c..6929d69b 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -336,7 +336,7 @@ class Query } } } else { - $prefix = $this->connection->getAttribute('prefix'); + $prefix = $this->connection->getConfig('prefix'); // 传入的表名为数组 if (is_array($join)) { if (0 !== $key = key($join)) {