增加db_attr_case配置参数 用于配置 数据表字段大小写规则

This commit is contained in:
thinkphp
2016-03-07 21:29:55 +08:00
parent e6bf88ce50
commit eb37deedd0
3 changed files with 21 additions and 20 deletions

View File

@@ -155,6 +155,7 @@ return [
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
'db_fields_strict' => true, 'db_fields_strict' => true,
'db_attr_case' => \PDO::CASE_LOWER,
'database' => [ 'database' => [
// 数据库类型 // 数据库类型
'type' => 'mysql', 'type' => 'mysql',

View File

@@ -33,7 +33,7 @@ class Model
// 数据库名称 // 数据库名称
protected $dbName = ''; protected $dbName = '';
// 数据表字段大小写 // 数据表字段大小写
protected $attrCase = \PDO::CASE_LOWER; protected $attrCase = null;
//数据库配置 //数据库配置
protected $connection = []; protected $connection = [];
// 数据表名(不包含表前缀) // 数据表名(不包含表前缀)
@@ -98,8 +98,8 @@ class Model
$this->dbName = $config['db_name']; $this->dbName = $config['db_name'];
} }
if (isset($config['attr_case'])) { if (is_null($this->attrCase)) {
$this->attrCase = $config['attr_case']; $this->attrCase = Config::get('db_attr_case');
} }
// 数据库初始化操作 // 数据库初始化操作

View File

@@ -933,13 +933,13 @@ abstract class Driver
* @param mixed $operator 运算符 * @param mixed $operator 运算符
* @param array $options 参数表达式 * @param array $options 参数表达式
**/ **/
public function updateFieldAll($field,$pk,$dataSet,$operator = '=',$options = []) public function updateFieldAll($field, $pk, $dataSet, $operator = '=', $options = [])
{ {
$values = []; $values = [];
$this->bind = array_merge($this->bind, !empty($options['bind']) ? $options['bind'] : []); $this->bind = array_merge($this->bind, !empty($options['bind']) ? $options['bind'] : []);
$field = $this->parseKey($field); $field = $this->parseKey($field);
$pk = $this->parseKey($pk); $pk = $this->parseKey($pk);
if(in_array($operator,['+','-'])){ if (in_array($operator, ['+', '-'])) {
$operator = '= ' . $field . $operator; $operator = '= ' . $field . $operator;
} }
@@ -953,18 +953,18 @@ abstract class Driver
if (0 === strpos($val, ':') && isset($this->bind[substr($val, 1)])) { if (0 === strpos($val, ':') && isset($this->bind[substr($val, 1)])) {
$value = $val; $value = $val;
} else { } else {
$name = count($this->bind); $name = count($this->bind);
$value = ':' . $_SERVER['REQUEST_TIME'] . '_' . $name; $value = ':' . $_SERVER['REQUEST_TIME'] . '_' . $name;
$this->bindParam( $_SERVER['REQUEST_TIME'] . '_' . $name, $val); $this->bindParam($_SERVER['REQUEST_TIME'] . '_' . $name, $val);
} }
} }
//没使用过非数字主键,怎么处理比较合适? //没使用过非数字主键,怎么处理比较合适?
$values[] = " WHEN " .$key." THEN " . $value; $values[] = " WHEN " . $key . " THEN " . $value;
} }
$sql = 'UPDATE ' . $this->parseTable($options['table']) . ' SET ' . $field . $operator . ' CASE ' . $pk . implode(' ', $values) . ' END ' ; $sql = 'UPDATE ' . $this->parseTable($options['table']) . ' SET ' . $field . $operator . ' CASE ' . $pk . implode(' ', $values) . ' END ';
//查询条件需和WHEN THEN对一致 //查询条件需和WHEN THEN对一致
$sql .= ' WHERE ' . $pk . ' in (' . implode(',',array_map([$this,'parseValue'],array_keys($dataSet))) . ')'; $sql .= ' WHERE ' . $pk . ' in (' . implode(',', array_map([$this, 'parseValue'], array_keys($dataSet))) . ')';
$sql .= $this->parseComment(!empty($options['comment']) ? $options['comment'] : ''); $sql .= $this->parseComment(!empty($options['comment']) ? $options['comment'] : '');
return $this->execute($sql, $this->getBindParams(true), !empty($options['fetch_sql']) ? true : false); return $this->execute($sql, $this->getBindParams(true), !empty($options['fetch_sql']) ? true : false);
} }