您的位置:首页 > 编程语言 > PHP开发

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐