### createBatch
**在数据表中批量新增数据**
**参数**
\* @param $row 数组形式,数组的键是数据表中的字段名,键对应的值是需要新增的数据。
\* @param $addObjs 数组形式,数组的键是数据表中的字段名,键对应的值是需要新增的数据。
\* @param $keyword 字符串类型,拼装SQL时,会检验数组中的$val\[$keyword\]中是否有值,没有则忽略该条数据
~~~
public function createBatch($rows, $addObjs = null, $keyword = null) {
if (! is_array ( $rows ) || count ( $rows ) == 0)
return FALSE;
$valArr = array ();
$cols = array ();
$row_frist = reset ( $rows );
// foreach ( $row_frist as $key => $value ) {
// $cols [] = $key;
// }
$cols = array();
foreach ( $rows as $key=>$row ) {
$row = $this->__prepera_format ( $row );
$rows[$key]=$row;
}
foreach ( $rows as $key => $value ) {
$keyarr = array_keys($value);
$cols=array_merge($cols,$keyarr);//合并所有数据列
}
$cols = array_unique($cols);
$col = join ( ',', $cols );
//判断是否有额外增加数组,有则加上
if (! empty ( $addObjs )) {
foreach ( $addObjs as $key => $value ) {
$col .= ',' . $key;
}
}
foreach ( $rows as $row ) {
if (! empty ( $keyword ) && empty ( $row [$keyword] )) {
continue;
}
$vals = array ();
//按输入字段来生成数组,不存在则生成空字符串(主要应对checkbox)
foreach ( $cols as $value ) {
if (isset ( $row [$value] )) {
$vals [] = "'" . $this->__val_escape ( $row [$value] ) . "'";
} else {
$vals [] = "''";
}
}
// foreach ( $row as $key => $value ) {
// $vals [] = "'" . $this->__val_escape ( $value ) . "'";
// }
//判断是否有额外数组,有则加上
if (! empty ( $addObjs )) {
foreach ( $addObjs as $key => $value ) {
$vals [] = "'" . $this->__val_escape ( $value ) . "'";
}
}
$valArr [] = "(" . join ( ',', $vals ) . ")";
}
if (empty ( $valArr )) {
return FALSE;
}
$val = implode ( ",", $valArr );
$sql = "INSERT INTO {$this->tbl_name} ({$col}) VALUES {$val}";
// echo $sql;
return $this->_db->query ( $sql );
}
~~~
- 架构
- 基础框架文档
- base/action(C层基类)
- protected 类定义
- __construct构造函数
- c_list
- c_index
- c_page
- c_pageJson
- c_listJson
- c_getByAjax
- c_getCountByName
- c_toAdd
- c_add
- c_init
- c_edit
- c_deletes
- c_ajaxdeletes
- c_saveBatch
- c_checkRepeat
- getDatadicts
- showDatadicts
- getDataNameByCode
- assign
- display
- view
- assignFunc
- model/base(M层基类)
- protected类定义(model)
- __construct
- find
- get_table_fields
- findAll
- findSql
- create
- delete
- findBy
- updateField
- query
- findCount
- update
- filterFunc
- filterField
- filterWithoutField
- filterCustom
- createBatch
- pageBySql
- listBySql
- pageBySqlId
- deletes
- 事务控制
- 获取对象数组
- addBatch_d
- add_d
- edit_d
- isRepeat
- 列表组件文档
- 基础表格
- 数据获取
- 表格数据显示
- 常用控制参数
- 表格右键扩展
- 表格扩展按钮
- 快速搜索
- 高级搜索(自定义视图)
- 表格注册事件
- 主从表格
- 可编辑表格
- 模块开发
- 开发案例
- 开发准则