public function insert(array $data) : array
    {
        $table = $this->definition->getName();
        $columns = $this->definition->getFullColumns();
        $insert = [];
        foreach ($data as $column => $value) {
            if (isset($columns[$column])) {
                $insert[$column] = $this->normalizeValue($columns[$column], $value);
            }
        }
        if (!count($insert)) {
            throw new DBException('No valid columns to insert');
        }
        $sql = 'INSERT INTO '.$table.' ('.implode(', ', array_keys($insert)).') VALUES (??)';
        $par = [$insert];
        $primary = $this->definition->getPrimaryKey();
        if (!count($primary)) {
            $this->db->query($sql, $par);
            return [];
        }
        if ($this->db->driverName() === 'oracle') {
            $ret = [];
            foreach ($primary as $k) {
                $ret[$k] = str_repeat(' ', 255);
                $par[] = &$ret[$k];
            }
            $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
            $this->db->query($sql, $par);
            return $ret;
        } else {
            $ret = [];
            $ins = $this->db->query($sql, $par)->insertID();
            foreach ($primary as $k) {
                $ret[$k] = $data[$k] ?? $ins;
            }
            return $ret;
        }
    }
public function insert(array $data) : array
    {
        $table = $this->definition->getName();
        $columns = $this->definition->getFullColumns();
        $insert = [];
        foreach ($data as $column => $value) {
            if (isset($columns[$column])) {
                $insert[$column] = $this->normalizeValue($columns[$column], $value);
            }
        }
        if (!count($insert)) {
            throw new DBException('No valid columns to insert');
        }
        $sql = 'INSERT INTO '.$table.' ('.implode(', ', array_keys($insert)).') VALUES (??)';
        $par = [$insert];
        $primary = $this->definition->getPrimaryKey();
        if (!count($primary)) {
            $this->db->query($sql, $par);
            return [];
        }
        if ($this->db->driverName() === 'oracle') {
            $ret = [];
            foreach ($primary as $k) {
                $ret[$k] = str_repeat(' ', 255);
                $par[] = &$ret[$k];
            }
            $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
            $this->db->query($sql, $par);
            return $ret;
        } else {
            $ret = [];
            $ins = $this->db->query($sql, $par)->insertID();
            foreach ($primary as $k) {
                $ret[$k] = $data[$k] ?? $ins;
            }
            return $ret;
        }
    }
public function insert ( array $ data ) : array { $ table = $ this -> definition -> getName ( ) ; $ columns = $ this -> definition -> getFullColumns ( ) ; $ insert = [ ] ; foreach ( $ data as $ column => $ value ) { if ( isset ( $ columns [ $ column ] ) ) { $ insert [ $ column ] = $ this -> normalizeValue ( $ columns [ $ column ] , $ value ) ; } } if ( ! count ( $ insert ) ) { throw new DBException ( 'No valid columns to insert' ) ; } $ sql = 'INSERT INTO ' . $ table . ' (' . implode ( ', ' , array_keys ( $ insert ) ) . ') VALUES (??)' ; $ par = [ $ insert ] ; $ primary = $ this -> definition -> getPrimaryKey ( ) ; if ( ! count ( $ primary ) ) { $ this -> db -> query ( $ sql , $ par ) ; return [ ] ; } if ( $ this -> db -> driverName ( ) === 'oracle' ) { $ ret = [ ] ; foreach ( $ primary as $ k ) { $ ret [ $ k ] = str_repeat ( ' ' , 255 ) ; $ par [ ] = & $ ret [ $ k ] ; } $ sql .= ' RETURNING ' . implode ( ',' , $ primary ) . ' INTO ' . implode ( ',' , array_fill ( 0 , count ( $ primary ) , '?' ) ) ; $ this -> db -> query ( $ sql , $ par ) ; return $ ret ; } else { $ ret = [ ] ; $ ins = $ this -> db -> query ( $ sql , $ par ) -> insertID ( ) ; foreach ( $ primary as $ k ) { $ ret [ $ k ] = $ data [ $ k ] ?? $ ins ; } return $ ret ; } }
Insert a new row in the table
@param  array   $data   key value pairs, where each key is the column name and the value is the value to insert
@return array           the inserted ID where keys are column names and values are column values
Insert a new row in the table