src/Services/PDOs/Builders/Builder.php
protected function doInsert(array $data = [])
    {
        if (empty($data)) {
            $this->insertEmptyRecord();
            return $this->db->lastInsertId();
        }

        if (!is_int(key($data))) {
            // single record is inserted
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], array_keys($data)));
            $params   = implode(', ', array_fill(0, count($data), '?'));
            $bindings = array_values($data);
        }
        else {
            // Bulk insert...
            $keys = [];
            foreach ($data as $row) {
                if (empty($row)) {
                    throw new InvalidArgumentException('Cannot insert an empty record on bulk mode.');
                }
                $keys = array_merge($keys, $row);
            }
            $keys     = array_keys($keys);
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], $keys));
            $params   = implode(', ', array_fill(0, count($keys), '?'));
            $bindings = [];
            $temp     = [];
            foreach ($data as $i => $row) {
                foreach ($keys as $key) {
                    $bindings[] = isset($row[$key]) ? $row[$key] : null;
                }
                $temp[] = $params;
            }
            $params = implode('), (', $temp);
        }

        $table = implode(', ', $this->from);

        /** @noinspection SqlDialectInspection */
        $query = "INSERT INTO $table ($columns) VALUES ($params)";

        $this->db->exec($query, $bindings);

        return $this->db->lastInsertId();
    }
protected function doInsert(array $data = [])
    {
        if (empty($data)) {
            $this->insertEmptyRecord();
            return $this->db->lastInsertId();
        }

        if (!is_int(key($data))) {
            // single record is inserted
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], array_keys($data)));
            $params   = implode(', ', array_fill(0, count($data), '?'));
            $bindings = array_values($data);
        }
        else {
            // Bulk insert...
            $keys = [];
            foreach ($data as $row) {
                if (empty($row)) {
                    throw new InvalidArgumentException('Cannot insert an empty record on bulk mode.');
                }
                $keys = array_merge($keys, $row);
            }
            $keys     = array_keys($keys);
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], $keys));
            $params   = implode(', ', array_fill(0, count($keys), '?'));
            $bindings = [];
            $temp     = [];
            foreach ($data as $i => $row) {
                foreach ($keys as $key) {
                    $bindings[] = isset($row[$key]) ? $row[$key] : null;
                }
                $temp[] = $params;
            }
            $params = implode('), (', $temp);
        }

        $table = implode(', ', $this->from);

        /** @noinspection SqlDialectInspection */
        $query = "INSERT INTO $table ($columns) VALUES ($params)";

        $this->db->exec($query, $bindings);

        return $this->db->lastInsertId();
    }
protected function doInsert ( array $ data = [ ] ) { if ( empty ( $ data ) ) { $ this -> insertEmptyRecord ( ) ; return $ this -> db -> lastInsertId ( ) ; } if ( ! is_int ( key ( $ data ) ) ) { // single record is inserted $ columns = implode ( ', ' , array_map ( [ $ this -> db , 'quoteName' ] , array_keys ( $ data ) ) ) ; $ params = implode ( ', ' , array_fill ( 0 , count ( $ data ) , '?' ) ) ; $ bindings = array_values ( $ data ) ; } else { // Bulk insert... $ keys = [ ] ; foreach ( $ data as $ row ) { if ( empty ( $ row ) ) { throw new InvalidArgumentException ( 'Cannot insert an empty record on bulk mode.' ) ; } $ keys = array_merge ( $ keys , $ row ) ; } $ keys = array_keys ( $ keys ) ; $ columns = implode ( ', ' , array_map ( [ $ this -> db , 'quoteName' ] , $ keys ) ) ; $ params = implode ( ', ' , array_fill ( 0 , count ( $ keys ) , '?' ) ) ; $ bindings = [ ] ; $ temp = [ ] ; foreach ( $ data as $ i => $ row ) { foreach ( $ keys as $ key ) { $ bindings [ ] = isset ( $ row [ $ key ] ) ? $ row [ $ key ] : null ; } $ temp [ ] = $ params ; } $ params = implode ( '), (' , $ temp ) ; } $ table = implode ( ', ' , $ this -> from ) ; /** @noinspection SqlDialectInspection */ $ query = "INSERT INTO $table ($columns) VALUES ($params)" ; $ this -> db -> exec ( $ query , $ bindings ) ; return $ this -> db -> lastInsertId ( ) ; }
Insert rows to the table and return the inserted autoincrement sequence value.

If you insert multiple rows, the method returns dependent to the driver the first or last inserted id!.

@param array $data Values to be updated
@return int|false
Insert rows to the table and return the inserted autoincrement sequence value .