Skip to content

Commit

Permalink
add example/insert-or-update-user-for-mysql.php
Browse files Browse the repository at this point in the history
  • Loading branch information
suin committed Dec 11, 2012
1 parent 8be967c commit 07bef70
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions example/insert-or-update-user-for-mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

require_once __DIR__.'/../vendor/autoload.php'; // load composer

use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
$pdo->query('CREATE TABLE IF NOT EXISTS user2 (id INT, `name` VARCHAR(255), email VARCHAR(255), PRIMARY KEY (`id`))');

$config = new LexerConfig();
$lexer = new Lexer($config);

$interpreter = new Interpreter();

$interpreter->addObserver(function(array $columns) use ($pdo) {
$stmt = $pdo->prepare('REPLACE user2 (id, name, email) VALUES (?, ?, ?)');
$stmt->execute($columns);
});

$lexer->parse('user.csv', $interpreter);
2 changes: 1 addition & 1 deletion src/Goodby/CSV/Import/Standard/Interpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function checkRowConsistency($line)
}

if ($current !== $this->rowConsistency) {
throw new StrictViolationException();
throw new StrictViolationException(sprintf('Column size should be %u, but %u columns given', $this->rowConsistency, $current));
}

$this->rowConsistency = $current;
Expand Down

0 comments on commit 07bef70

Please sign in to comment.