DboSource::insertMulti PHP Method

insertMulti() public method

Inserts multiple values into a table
public insertMulti ( string $table, array $fields, array $values ) : boolean
$table string The table being inserted into.
$fields array The array of field/column names being inserted.
$values array The array of values to insert. The values should be an array of rows. Each row should have values keyed by the column name. Each row must have the values in the same order as $fields.
return boolean
    public function insertMulti($table, $fields, $values)
    {
        $table = $this->fullTableName($table);
        $holder = implode(',', array_fill(0, count($fields), '?'));
        $fields = implode(', ', array_map(array(&$this, 'name'), $fields));
        $pdoMap = array('integer' => PDO::PARAM_INT, 'float' => PDO::PARAM_STR, 'boolean' => PDO::PARAM_BOOL, 'string' => PDO::PARAM_STR, 'text' => PDO::PARAM_STR);
        $columnMap = array();
        $sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holder})";
        $statement = $this->_connection->prepare($sql);
        $this->begin();
        foreach ($values[key($values)] as $key => $val) {
            $type = $this->introspectType($val);
            $columnMap[$key] = $pdoMap[$type];
        }
        foreach ($values as $value) {
            $i = 1;
            foreach ($value as $col => $val) {
                $statement->bindValue($i, $val, $columnMap[$col]);
                $i += 1;
            }
            $statement->execute();
            $statement->closeCursor();
            if ($this->fullDebug) {
                $this->logQuery($sql, $value);
            }
        }
        return $this->commit();
    }

Usage Example

Example #1
0
 /**
  * Inserts multiple values into a table
  *
  * @param string $table
  * @param string $fields
  * @param array $values
  * @access protected
  */
 function insertMulti($table, $fields, $values)
 {
     $primaryKey = $this->_getPrimaryKey($table);
     $hasPrimaryKey = $primaryKey != null && (is_array($fields) && in_array($primaryKey, $fields) || is_string($fields) && strpos($fields, $this->startQuote . $primaryKey . $this->endQuote) !== false);
     if ($hasPrimaryKey) {
         $this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' ON');
     }
     parent::insertMulti($table, $fields, $values);
     if ($hasPrimaryKey) {
         $this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' OFF');
     }
 }
All Usage Examples Of DboSource::insertMulti
DboSource