53 lines
1.3 KiB
PHP
53 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use DateTime;
|
|
|
|
class WarrantySerialLoadLogger
|
|
{
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function logWarrantySerialLoadInfo($log_data)
|
|
{
|
|
// cache directory
|
|
$cache_dir = __DIR__ . '/../../var/cache';
|
|
|
|
$file = $cache_dir . '/warranty_serial_load_log.tab';
|
|
// error_log('opening file for warranty serial load log - ' . $file);
|
|
|
|
$fp = fopen($file, 'w');
|
|
if ($fp === false)
|
|
{
|
|
error_log('could not open file for load data infile - ' . $file);
|
|
}
|
|
else
|
|
{
|
|
foreach ($log_data as $key => $data)
|
|
{
|
|
$line = implode('|', $data) . "\r\n";
|
|
fwrite($fp, $line);
|
|
}
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
// prepared statement
|
|
$db = $this->em->getConnection();
|
|
$stmt = $db->prepare('LOAD DATA LOCAL INFILE \''. $file . '\' INTO TABLE warranty_serial_load_log FIELDS TERMINATED BY \'|\' LINES TERMINATED BY \'\\r\\n\' (date_create, api_user, serial, flag_loaded, error)');
|
|
|
|
$result = $stmt->execute();
|
|
if (!$result)
|
|
error_log('Failed loading data.');
|
|
|
|
// TODO: delete file?
|
|
}
|
|
|
|
}
|