doctrine-bulk-insert icon indicating copy to clipboard operation
doctrine-bulk-insert copied to clipboard

I dont understand how to use types method

Open solovichenko opened this issue 4 years ago • 1 comments

I write this for have available use types in bulk insert:

private function types(array $types, int $datasetLength, array $columns): array
    {
        $positionalTypes = [];

        for ($dataRowIdx = 0; $dataRowIdx < $datasetLength; $dataRowIdx++) {
            foreach (array_values($columns) as $columnIndex => $column) {
                $columnIndex = $columnIndex + $dataRowIdx * count($columns);
                $positionalTypes[$columnIndex] = null;
                if (array_key_exists($column, $types)) {
                    $positionalTypes[$columnIndex] = $types[$column];
                }
            }
        }

        return $positionalTypes;
    }

And

return $this->connection->executeUpdate($sql, $this->parameters($dataset), $this->types($types, count($dataset), $this->extractColumns($dataset)));

solovichenko avatar Apr 13 '21 21:04 solovichenko

@solovichenko, if I get you right, this is what you want:

<?php

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Types;
use Franzose\DoctrineBulkInsert\Query;

$connection = new Connection(...);

$rows = (new Query($connection))->execute(
    'table',
    [
       ['foo' => 111, 'bar' => 'qux'],
       ['foo' => 333, 'bar' => 'doo'],
    ],
    [
        Types::BIGINT, // 'foo' is bigint
        Types::TEXT // 'bar' is text 
    ]
);

I need to add this to the README. The thing is that the third argument is used the same as in Dotrine DBAL (so-called parallel arrays).

franzose avatar Jan 27 '22 10:01 franzose