Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I dont understand how to use types method #4

Open
solovichenko opened this issue Apr 13, 2021 · 1 comment
Open

I dont understand how to use types method #4

solovichenko opened this issue Apr 13, 2021 · 1 comment

Comments

@solovichenko
Copy link

solovichenko commented Apr 13, 2021

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)));
@franzose
Copy link
Owner

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants