public function insert(array $fields, $table, $updateOnDuplicate = false)
    {
        // Multiple Insert
        if (is_array(current($fields))) {
            $sql  = "INSERT INTO `$table` (`".implode('`, `', array_keys(current($fields))).'`) VALUES ';
            $rows = array();

            foreach ($fields as $key => $array) {
                // Sanity check: Make sure we dont end up with ',()' in the SQL.
                if (!is_array($array)) {
                    continue;
                }

                self::cleanFields($array);
                $rows[] = '('.implode(', ', $array).')';
            }

            $sql .= implode(", ", $rows);

            // Single Insert
        } else {
            self::cleanFields($fields);
            $sql  = "INSERT INTO `$table` (`".implode('`, `', array_keys($fields)).'`) VALUES ('.implode(', ', $fields).')';

            if ($updateOnDuplicate) {
                $sql .= ' ON DUPLICATE KEY UPDATE ';

                foreach ($fields as $key => $value) {
                    $sql .= " `$key` = $value,";
                }

                $sql = trim($sql, ',');
            }
        }

        return $this->query($sql);
    }
public function insert(array $fields, $table, $updateOnDuplicate = false)
    {
        // Multiple Insert
        if (is_array(current($fields))) {
            $sql  = "INSERT INTO `$table` (`".implode('`, `', array_keys(current($fields))).'`) VALUES ';
            $rows = array();

            foreach ($fields as $key => $array) {
                // Sanity check: Make sure we dont end up with ',()' in the SQL.
                if (!is_array($array)) {
                    continue;
                }

                self::cleanFields($array);
                $rows[] = '('.implode(', ', $array).')';
            }

            $sql .= implode(", ", $rows);

            // Single Insert
        } else {
            self::cleanFields($fields);
            $sql  = "INSERT INTO `$table` (`".implode('`, `', array_keys($fields)).'`) VALUES ('.implode(', ', $fields).')';

            if ($updateOnDuplicate) {
                $sql .= ' ON DUPLICATE KEY UPDATE ';

                foreach ($fields as $key => $value) {
                    $sql .= " `$key` = $value,";
                }

                $sql = trim($sql, ',');
            }
        }

        return $this->query($sql);
    }
public function insert ( array $ fields , $ table , $ updateOnDuplicate = false ) { // Multiple Insert if ( is_array ( current ( $ fields ) ) ) { $ sql = "INSERT INTO `$table` (`" . implode ( '`, `' , array_keys ( current ( $ fields ) ) ) . '`) VALUES ' ; $ rows = array ( ) ; foreach ( $ fields as $ key => $ array ) { // Sanity check: Make sure we dont end up with ',()' in the SQL. if ( ! is_array ( $ array ) ) { continue ; } self :: cleanFields ( $ array ) ; $ rows [ ] = '(' . implode ( ', ' , $ array ) . ')' ; } $ sql .= implode ( ", " , $ rows ) ; // Single Insert } else { self :: cleanFields ( $ fields ) ; $ sql = "INSERT INTO `$table` (`" . implode ( '`, `' , array_keys ( $ fields ) ) . '`) VALUES (' . implode ( ', ' , $ fields ) . ')' ; if ( $ updateOnDuplicate ) { $ sql .= ' ON DUPLICATE KEY UPDATE ' ; foreach ( $ fields as $ key => $ value ) { $ sql .= " `$key` = $value," ; } $ sql = trim ( $ sql , ',' ) ; } } return $ this -> query ( $ sql ) ; }
A convenience method to insert data into the Database. This function
takes an associative array of data to input, with the keys being the column
names and the table. An optional parameter exposes MySQL's ON DUPLICATE
KEY UPDATE functionality, which will update the values if a duplicate key
is found.

@param array $fields
An associative array of data to input, with the key's mapping to the
column names. Alternatively, an array of associative array's can be
provided, which will perform multiple inserts
@param string $table
The table name, including the tbl prefix which will be changed
to this Symphony's table prefix in the query function
@param boolean $updateOnDuplicate
If set to true, data will updated if any key constraints are found that cause
conflicts. By default this is set to false, which will not update the data and
would return an SQL error
@throws DatabaseException
@return boolean
A convenience method to insert data into the Database . This function takes an associative array of data to input with the keys being the column names and the table . An optional parameter exposes MySQL s ON DUPLICATE KEY UPDATE functionality which will update the values if a duplicate key is found .