src/PDO/SchemaPDOMssql.php
/**
     * Executes the INSERT command, returning primary key values.
     *
     * @param string $table the table that new rows will be inserted into.
     * @param array $columns the column data (name => value) to be inserted into the table.
     *
     * @throws Exception|InvalidCallException|InvalidConfigException|Throwable
     *
     * @return array|false primary key values or false if the command fails.
     */
Executes the INSERT command, returning primary key values.
@param string $table the table that new rows will be inserted into.
@param array $columns the column data (name => value) to be inserted into the table.



@return array|false primary key values or false if the command fails.
Executes the INSERT command returning primary key values . @param string $table the table that new rows will be inserted into . @param array $columns the column data ( name = > value ) to be inserted into the table . @return array|false primary key values or false if the command fails .
public function insert(string $table, array $columns): bool|array
    {
        $command = $this->db->createCommand()->insert($table, $columns);
        if (!$command->execute()) {
            return false;
        }
        $isVersion2005orLater = version_compare($this->db->getServerVersion(), '9', '>=');
        $inserted = $isVersion2005orLater ? $command->getPdoStatement()->fetch() : [];
        $tableSchema = $this->getTableSchema($table);
        $result = [];
        foreach ($tableSchema->getPrimaryKey() as $name) {
            if (isset($inserted[$name])) {
                $result[$name] = $inserted[$name];
            } elseif ($tableSchema->getColumns()[$name]->isAutoIncrement()) {
                $result[$name] = $this->getLastInsertID($tableSchema->getSequenceName());
            } elseif (isset($columns[$name])) {
                $result[$name] = $columns[$name];
            } else {
                $result[$name] = $tableSchema->getColumns()[$name]->getDefaultValue();
            }
        }
        return $result;
    }
public function insert ( string $ table , array $ columns ) : bool | array { $ command = $ this -> db -> createCommand ( ) -> insert ( $ table , $ columns ) ; if ( ! $ command -> execute ( ) ) { return false ; } $ isVersion2005orLater = version_compare ( $ this -> db -> getServerVersion ( ) , '9' , '>=' ) ; $ inserted = $ isVersion2005orLater ? $ command -> getPdoStatement ( ) -> fetch ( ) : [ ] ; $ tableSchema = $ this -> getTableSchema ( $ table ) ; $ result = [ ] ; foreach ( $ tableSchema -> getPrimaryKey ( ) as $ name ) { if ( isset ( $ inserted [ $ name ] ) ) { $ result [ $ name ] = $ inserted [ $ name ] ; } elseif ( $ tableSchema -> getColumns ( ) [ $ name ] -> isAutoIncrement ( ) ) { $ result [ $ name ] = $ this -> getLastInsertID ( $ tableSchema -> getSequenceName ( ) ) ; } elseif ( isset ( $ columns [ $ name ] ) ) { $ result [ $ name ] = $ columns [ $ name ] ; } else { $ result [ $ name ] = $ tableSchema -> getColumns ( ) [ $ name ] -> getDefaultValue ( ) ; } } return $ result ; }
Executes the INSERT command, returning primary key values.
Executes the INSERT command returning primary key values .
/**
             * {@see https://github.com/yiisoft/yii2/issues/13828 & https://github.com/yiisoft/yii2/issues/17474
             */ // for a version earlier than 2005