mixpo/form-data-exporter

简单的数据库导出库 -> CSV

v2.1.5 2015-10-14 22:53 UTC

README

这是一个专门用于将 SQL 结果集中的所有行导出到 CSV 的 JSON 数据列的工具。

状态

Build Status

Coverage Status

安装

composer install

phpunit

用法

<?php

$exporter = new FormExporter(
    'dsn', // <- PDO DSN string including credentials
    'tableName', // <- the table the you wish to export data from
    'data', // <- the name of the 'data' column (column that hold the JSON form data)
    ["client_id" => "12345", "campaign" => "widget-2015"], // <- criteria used to build an AND'ed WHERE clause
    $logger // <- Psr\Log\LoggerInterface
);

然后设置写入引擎(目前支持文件系统和 S3,并执行导出

$exporter->setExporterEngine(
    new FileSystemExporterEngine(
       'file:///var/data/csv/leadgen99.csv',  // <- full path to the output CSV in <protocol>://<path> format
                                                    s3://<bucket>/<object-path> is also supported
        $logger, // <- Psr\Log\LoggerInterface
        $randomizeFileName = true
    )
);
                                              
$outputCsvFilePath = $exporter->run();

注意 在上面的示例中,$outputCsvFilePath 将包含一个随机种子。例如,将 file:///var/data/csv/leadgen99.csv 发送到 ExporterEngine 构造函数中,$outputCsvFilePath 将类似于:/var/data/csv/leadgen99-55358afdeefa5.csv

要关闭此行为,请调用 set $randomizeFileName = false

存在问题的行

在调用 $exporter->run() 后,您可以调用 getIssues(),它将返回一个包含任何解析问题的行的数组,这些行将不会出现在 CSV 输出中。

<?php

$exporter->run();
$issues = $exporter->getIssues();

/**
 * $issues will be an empty array if there were no issues, but if there were it will look something like this.
 *      [
 *       [
 *         "message" => "Expected data field of name: 'form_data', not found for this row",
 *         "row" => ["id" => 2, 
 *                   "identifier" => "client-xyz",
 *                   "tag" => "widget-1-campaign",
 *                   "version" => 1,
 *                   "created" => "2015-04-16 21:50:39"]
 *       ],
 *       ...
 *      ]
 **/