改进insertall方法

This commit is contained in:
thinkphp
2016-04-16 16:42:10 +08:00
parent 09d5fb734c
commit b97df6289e

View File

@@ -59,8 +59,7 @@ abstract class Builder
* 数据分析 * 数据分析
* @access protected * @access protected
* @param array $data 数据 * @param array $data 数据
* @param array $bind 参数绑定类型 * @param array $options 查询参数
* @param string $type insert update
* @return array * @return array
*/ */
protected function parseData($data, $options) protected function parseData($data, $options)
@@ -68,6 +67,7 @@ abstract class Builder
if (empty($data)) { if (empty($data)) {
return []; return [];
} }
// 获取绑定信息 // 获取绑定信息
$bind = $this->connection->getTableInfo($options['table'], 'bind'); $bind = $this->connection->getTableInfo($options['table'], 'bind');
$fields = array_keys($bind); $fields = array_keys($bind);
@@ -84,9 +84,13 @@ abstract class Builder
} elseif (is_null($val)) { } elseif (is_null($val)) {
$result[$item] = 'NULL'; $result[$item] = 'NULL';
} elseif (is_scalar($val)) { } elseif (is_scalar($val)) {
// 过滤非标量数据 if ($bindValue) {
$this->query->bind($key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR); // 过滤非标量数据
$result[$item] = ':' . $key; $this->query->bind($key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
$result[$item] = ':' . $key;
} else {
$result[$item] = $this->parseValue($val);
}
} }
} }
} }
@@ -543,14 +547,24 @@ abstract class Builder
*/ */
public function insertAll($dataSet, $options) public function insertAll($dataSet, $options)
{ {
$fields = array_map([$this, 'parseKey'], array_keys($dataSet[0])); // 获取绑定信息
foreach ($dataSet as $data) { $fields = $this->connection->getTableInfo($options['table'], 'fields');
foreach ($dataSet as &$data) {
foreach ($data as $key => $val) {
if (!in_array($key, $fields, true)) {
if ($this->connection->getAttribute('fields_strict')) {
throw new Exception(' fields not exists :[' . $key . ']');
}
unset($data[$key]);
} else {
$data[$key] = $this->parseValue($val);
}
}
$value = array_values($data); $value = array_values($data);
$value = array_map([$this, 'parseValue'], $value);
$values[] = 'SELECT ' . implode(',', $value); $values[] = 'SELECT ' . implode(',', $value);
} }
$fields = array_map([$this, 'parseKey'], array_keys($dataSet[0]));
$sql = str_replace( $sql = str_replace(
['%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'], ['%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
[ [
$this->parseTable($options['table']), $this->parseTable($options['table']),