Skip to content

Instantly share code, notes, and snippets.

@nini
Last active July 12, 2017 06:54
Show Gist options
  • Save nini/a66ef99e04efb38c5d41a65b58f519f1 to your computer and use it in GitHub Desktop.
Save nini/a66ef99e04efb38c5d41a65b58f519f1 to your computer and use it in GitHub Desktop.
Postgres/Spot2/DBAL BLOB read/write
<?php
//read
$conn = $this->getMapper()->connection();
$first = $this->getMapper()->first();
$db = $conn->getWrappedConnection(); //pdo
$conn->beginTransaction();
$stream = $db->pgsqlLOBOpen($first->getContent(), 'r');
$i = 0;
while (($row = fgetcsv($stream)) !== false) {
$i++;
dump($row);
}
dump($i);
return;
//write
$fh = fopen(urldecode($file), 'rb');
$db = $this->getMapper()->connection()->getWrappedConnection();//pdo
$db->beginTransaction();
$oid = $db->pgsqlLOBCreate();
$stream = $db->pgsqlLOBOpen($oid, 'w');
$local = $fh;
stream_copy_to_stream($local, $stream);
$local = null;
$stream = null;
$stmt = $db->prepare("INSERT INTO import (content) VALUES (?)");
$stmt->execute(array($oid));
$db->commit();
fclose($fh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment