src/Titon/Db/Repository.php
public function createMany(array $data, $allowPk = false, array $options = []) {
        $pk = $this->getPrimaryKey();
        $columns = $this->getSchema()->getColumns();
        $records = [];
        $defaults = [];

        if ($columns) {
            foreach ($columns as $key => $column) {
                $defaults[$key] = array_key_exists('default', $column) ? $column['default'] : '';
            }

            unset($defaults[$pk]);
        }

        foreach ($data as $record) {

            // Convert from an entity
            if ($record instanceof Arrayable) {
                $record = $record->toArray();
            }

            // Merge in defaults
            $record = Hash::merge($defaults, $record);

            // Remove primary key
            if (!$allowPk) {
                unset($record[$pk]);
            }

            // Filter out invalid columns
            if ($columns) {
                $record = array_intersect_key($record, $columns);
            }

            $records[] = $record;
        }

        return $this->query(Query::MULTI_INSERT)->save($records, $options);
    }
public function createMany(array $data, $allowPk = false, array $options = []) {
        $pk = $this->getPrimaryKey();
        $columns = $this->getSchema()->getColumns();
        $records = [];
        $defaults = [];

        if ($columns) {
            foreach ($columns as $key => $column) {
                $defaults[$key] = array_key_exists('default', $column) ? $column['default'] : '';
            }

            unset($defaults[$pk]);
        }

        foreach ($data as $record) {

            // Convert from an entity
            if ($record instanceof Arrayable) {
                $record = $record->toArray();
            }

            // Merge in defaults
            $record = Hash::merge($defaults, $record);

            // Remove primary key
            if (!$allowPk) {
                unset($record[$pk]);
            }

            // Filter out invalid columns
            if ($columns) {
                $record = array_intersect_key($record, $columns);
            }

            $records[] = $record;
        }

        return $this->query(Query::MULTI_INSERT)->save($records, $options);
    }
public function createMany ( array $ data , $ allowPk = false , array $ options = [ ] ) { $ pk = $ this -> getPrimaryKey ( ) ; $ columns = $ this -> getSchema ( ) -> getColumns ( ) ; $ records = [ ] ; $ defaults = [ ] ; if ( $ columns ) { foreach ( $ columns as $ key => $ column ) { $ defaults [ $ key ] = array_key_exists ( 'default' , $ column ) ? $ column [ 'default' ] : '' ; } unset ( $ defaults [ $ pk ] ) ; } foreach ( $ data as $ record ) { // Convert from an entity if ( $ record instanceof Arrayable ) { $ record = $ record -> toArray ( ) ; } // Merge in defaults $ record = Hash :: merge ( $ defaults , $ record ) ; // Remove primary key if ( ! $ allowPk ) { unset ( $ record [ $ pk ] ) ; } // Filter out invalid columns if ( $ columns ) { $ record = array_intersect_key ( $ record , $ columns ) ; } $ records [ ] = $ record ; } return $ this -> query ( Query :: MULTI_INSERT ) -> save ( $ records , $ options ) ; }
Insert multiple records into the database using a single query.
Missing fields will be added with an empty value or the schema default value.
Does not support callbacks or transactions.

@uses Titon\Utility\Hash

@param array $data Multi-dimensional array of records
@param bool $allowPk If true will allow primary key fields, else will remove them
@param array $options
@return int The count of records inserted
Insert multiple records into the database using a single query . Missing fields will be added with an empty value or the schema default value . Does not support callbacks or transactions .