Zend Framework 2 支持同时插入多条数据的Insert扩展
2014-10-30 17:50
525 查看
Zend Framework 2的Insert方法默认情况下一次只能进行一条数据的插入,本人仿照其Insert写了一个简单的Insert类,本类还有许多的不足,只是提供一个思路。
好了不多说直接上代码。
使用方法:
好了不多说直接上代码。
<?php /** * ZF2 能同时插入多条数据的简单Insert * * @author Star <wmistar@gmail.com> * @license http://mushroot.com * @version: 1.0 * */ namespace Application\Model\Sql; use Zend\Db\Adapter\Adapter; class YsInsert { private $table = NULL; private $rows = NULL; private $columns = NULL; private $adapter = NULL; private $values = array(); protected $insert = 'INSERT INTO %1$s (%2$s) VALUES %3$s'; public function __construct(Adapter $adapter) { $this->rows = NULL; $this->adapter = $adapter; } /** * 设置当前表 * * @param string $table */ public function into($table) { $this->table = $table; } /** * 指定列 * * @param array $columns * @return \Guide\Model\YsInsert */ public function columns(array $columns) { $this->columns = $columns; return $this; } /** * 添加要插入的参数 * * @param array $rows * @throws \InvalidArgumentException * @return \Guide\Model\YsInsert */ public function addRows(array $rows) { if ($rows == null) { throw new \InvalidArgumentException('addRows() 需要传个数组进去!'); } $keys = array_keys($rows); $firstKey = current($keys); $tmpArr = array(); //判断是否有默认键 if (is_string($firstKey)) { foreach ($keys as $key) { if (($index = array_search($key, $this->columns)) !== false) { $tmpArr[$index] = $rows[$key]; }else { $this->columns[] = $key; $tmpArr = $rows[$key]; } } $this->values[] = $tmpArr; } elseif (is_int($firstKey)) { $this->values[] = array_values($rows); } return $this; } /** * 执行Insert操作 * * @param Adapter $dbadapter */ public function execute() { $table = $this->table; $columns = $this->columns; $columns = array(); $values = array(); $data = array(); //按行生成占位符以及参数数组 foreach ($this->values as $cValue){ $tColumns = $this->columns; //根据行生成临时参数 foreach ($this->columns as $cIndex => $column) { //判断是否存在当前参数,若存在则替换临时参数中的数据,否则赋空值 if ( isset($cValue[$cIndex]) ) { $tColumns[$cIndex] = $cValue[$cIndex]; }else{ $tColumns[$cIndex] = null; } $tArrHolder[$cIndex] = '?'; //生成占位符数组 } $valueHolder[] = '('.implode(',', $tArrHolder).')'; //生成占位符字符串 $tempValues[] = $tColumns; } //将多个含参数的数组合并成一个 foreach ($tempValues as $arr){ $values = array_merge_recursive($values, $arr); } $sql = sprintf( $this->insert, $table, implode(', ', $columns), implode(', ', $valueHolder) ); return $this->adapter->query($sql, $values); } }
使用方法:
$insert = new YsInsert($adapter); $insert->into('user_table') ->columns(array('id','username')) ->addRows(array(1, '土豆')) ->addRows(array(2, '地瓜')); $resolut = $insert->execute();
相关文章推荐
- EntityFramework.BulkInsert扩展插入数据和EF本身插入数据比较
- 使用template扩展已有的DDX_Text函数,使扩展能够同时支持各种数据类型的一个例子
- EntityFramework.BulkInsert扩展插入数据和EF本身插入数据比较
- PG(HGDB)支持在同一个insert语句中插入多行数据
- 使用template扩展已有的DDX_Text函数,使扩展能够同时支持各种数据类型的一个例子
- 如何往有自增标识字段的表插入数据时,同时给自增标识字段插入值呢,在Inset Into语句前后加上SQL语句:SET IDENTITY_INSERT TableName ON和SET IDENTITY_INSERT TableName OFF
- 用C#同时向SQL Server插入多行数据
- informix 建表初始空间和扩展空间设置不合理导致插入数据时锁表问题.
- TextBox的扩展,支持文件夹和各类文件的拖入,同时可设置过滤文件格式!
- 使用insert向多表插入数据
- c#批量插入数据到数据库【支持事务操作】
- EXCEL表里的数据转换成insert into tablename(field)values() SQL插入语句
- 批量更新数据(batches update )插入数据(batches insert)
- insert into table 插入多条数据
- 使用Bulk Insert插入大批量数据
- 怎样将数据同时插入两个表中(事物开始后,插入语句后,自动增长列有没有生成一个值,有的话,值怎样获得)
- 关于ibatis insert 返回值为 null的研究----得到插入数据的逐渐值
- TextBox的扩展,支持文件夹和各类文件的拖入,同时可设置过滤文件格式!
- 关于含有标识字段的数据表中记录的插入问题解决(IDENTITY_INSERT)
- ASP 获取 ACCESS 中新插入数据的 ID 或多个ID /Getting last insert ID/ By Stabx