From a08967d82567225fdf0f3e40dca620621973567e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 5 Aug 2016 13:48:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=AE=BE=E5=AE=9A=20=E4=B8=BB=E9=94=AE=E3=80=81?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=BF=A1=E6=81=AF=E5=92=8C=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=20=E4=B8=8D=E4=BE=9D=E8=B5=96=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 21 ++++-- library/think/db/Builder.php | 14 ++-- library/think/db/Query.php | 125 +++++++++++++++++++++++++++-------- 3 files changed, 121 insertions(+), 39 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 16db8797..12e34bf5 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -52,16 +52,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $class; // 回调事件 private static $event = []; - - // 数据表主键 复合主键使用数组定义 - protected $pk; // 错误信息 protected $error; // 字段验证规则 protected $validate; - + // 数据表主键 复合主键使用数组定义 + protected $pk; // 字段属性 protected $field = []; + // 字段类型 + protected $fieldType = []; // 显示属性 protected $visible = []; // 隐藏属性 @@ -155,6 +155,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } else { $query->name($this->name); } + + if (!empty($this->field)) { + $query->allowField($this->field); + } + + if (!empty($this->fieldType)) { + $query->setFieldType($this->fieldType); + } + + if (!empty($this->pk)) { + $query->pk($this->pk); + } + // 全局作用域 if (method_exists($this, 'base')) { call_user_func_array([$this, 'base'], [ & $query]); diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 1024521c..3ebdb692 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -83,7 +83,7 @@ abstract class Builder } // 获取绑定信息 - $bind = $this->query->getTableInfo($options['table'], 'bind'); + $bind = $this->query->getFieldsBind($options); if ('*' == $options['field']) { $fields = array_keys($bind); } else { @@ -228,8 +228,8 @@ abstract class Builder $whereStr = ''; // 获取字段信息 - $fields = $this->query->getTableInfo($options['table'], 'fields'); - $binds = $this->query->getTableInfo($options['table'], 'bind'); + $fields = $this->query->getTableFields($options); + $binds = $this->query->getFieldsBind($options); foreach ($where as $key => $val) { $str = []; foreach ($val as $field => $value) { @@ -369,10 +369,8 @@ abstract class Builder protected function parseDateTime($value, $key, $options = []) { // 获取时间字段类型 - $type = $this->query->getTableInfo($options['table'], 'type'); - if (isset($options['field_type'][$key])) { - $info = $options['field_type'][$key]; - } elseif (isset($type[$key])) { + $type = $this->query->getFieldsType($options); + if (isset($type[$key])) { $info = $type[$key]; } if (isset($info)) { @@ -608,7 +606,7 @@ abstract class Builder { // 获取合法的字段 if ('*' == $options['field']) { - $fields = $this->query->getTableInfo($options['table'], 'fields'); + $fields = $this->query->getFieldsType($options); } else { $fields = $options['field']; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 2fab3b17..d0aa4f1f 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -720,11 +720,11 @@ class Query } if (true === $field) { // 获取全部字段 - $fields = $this->getTableInfo($tableName, 'fields'); + $fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields'); $field = $fields ?: ['*']; } elseif ($except) { // 字段排除 - $fields = $this->getTableInfo($tableName, 'fields'); + $fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields'); $field = $fields ? array_diff($fields, $field) : $field; } if ($tableName) { @@ -1235,6 +1235,47 @@ class Query return $this; } + /** + * 设置数据表字段 + * @access public + * @param string|array $field 字段信息 + * @return $this + */ + public function allowField($field) + { + if (true === $field) { + $field = $this->getTableInfo('', 'fields'); + } elseif (is_string($field)) { + $field = explode(',', $field); + } + $this->options['allow_field'] = $field; + return $this; + } + + /** + * 设置字段类型 + * @access public + * @param array $fieldType 字段类型信息 + * @return $this + */ + public function setFieldType($fieldType = []) + { + $this->options['field_type'] = $fieldType; + return $this; + } + + /** + * 指定数据表主键 + * @access public + * @param string $pk 主键 + * @return $this + */ + public function pk($pk) + { + $this->options['pk'] = $pk; + return $this; + } + /** * 查询日期或者时间 * @access public @@ -1284,18 +1325,6 @@ class Query return $this; } - /** - * 设置字段类型 - * @access public - * @param array $fieldType 字段类型信息 - * @return $this - */ - public function setFieldType($fieldType = []) - { - $this->options['field_type'] = $fieldType; - return $this; - } - /** * 获取数据表信息 * @access public @@ -1327,13 +1356,7 @@ class Query foreach ($info as $key => $val) { // 记录字段类型 $type[$key] = $val['type']; - if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $val['type'])) { - $bind[$key] = PDO::PARAM_INT; - } elseif (preg_match('/bool/is', $val['type'])) { - $bind[$key] = PDO::PARAM_BOOL; - } else { - $bind[$key] = PDO::PARAM_STR; - } + $bind[$key] = $this->getFieldBindType($val['type']); if (!empty($val['primary'])) { $pk[] = $key; } @@ -1352,12 +1375,60 @@ class Query /** * 获取当前数据表的主键 * @access public - * @param string $table 数据表名 + * @param string|array $options 数据表名或者查询参数 * @return string|array */ - public function getPk($table = '') + public function getPk($options = '') { - return $this->getTableInfo($table, 'pk'); + if (!empty($options['pk'])) { + $pk = $options['pk']; + } elseif (isset($this->options['pk'])) { + $pk = $this->options['pk']; + } else { + $pk = $this->getTableInfo(is_array($options) ? $options['table'] : $options, 'pk'); + } + return $pk; + } + + // 获取当前数据表字段信息 + public function getTableFields($options) + { + return !empty($options['allow_field']) ? $options['allow_field'] : $this->getTableInfo($options['table'], 'fields'); + } + + // 获取当前数据表字段类型 + public function getFieldsType($options) + { + return !empty($options['field_type']) ? $options['field_type'] : $this->getTableInfo($options['table'], 'type'); + } + + // 获取当前数据表绑定信息 + public function getFieldsBind($options) + { + $types = $this->getFieldsType($options); + $bind = []; + foreach ($types as $key => $type) { + $bind[$key] = $this->getFieldBindType($type); + } + return $bind; + } + + /** + * 获取字段绑定类型 + * @access public + * @param string $type 字段类型 + * @return integer + */ + protected function getFieldBindType($type) + { + if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $type)) { + $bind = PDO::PARAM_INT; + } elseif (preg_match('/bool/is', $type)) { + $bind = PDO::PARAM_BOOL; + } else { + $bind = PDO::PARAM_STR; + } + return $bind; } /** @@ -1546,7 +1617,7 @@ class Query */ protected function parsePkWhere($data, &$options) { - $pk = $this->getPk($options['table']); + $pk = $this->getPk($options); // 获取当前数据表 if (!empty($options['alias'])) { $alias = $options['alias']; @@ -1684,7 +1755,7 @@ class Query { $options = $this->parseExpress(); if (empty($options['where'])) { - $pk = $this->getPk($options['table']); + $pk = $this->getPk($options); // 如果存在主键数据 则自动作为更新条件 if (is_string($pk) && isset($data[$pk])) { $where[$pk] = $data[$pk]; @@ -1951,8 +2022,8 @@ class Query */ public function chunk($count, $callback, $column = null) { - $column = $column ?: $this->getPk(); $options = $this->getOptions(); + $column = $column ?: ($options['pk'] ?: $this->getPk()); $bind = $this->bind; $resultSet = $this->limit($count)->order($column, 'asc')->select();