DataExporter
in package
Exports database data in various formats Supports SQL, JSON, CSV, and YAML output formats
Table of Contents
Constants
- FORMAT_CSV = 'csv'
- FORMAT_JSON = 'json'
- FORMAT_SQL = 'sql'
- FORMAT_YAML = 'yaml'
Properties
- $_Adapter : AdapterInterface
- $_AdapterType : string
- $_MigrationTable : string
- $_Options : array<string|int, mixed>
- $fs : IFileSystem
Methods
- __construct() : mixed
- __destruct() : mixed
- Destructor - ensure adapter is disconnected
- disconnect() : void
- Disconnect from database
- export() : string
- Export data to string
- exportCsvToDirectory() : array<string|int, mixed>
- Export all tables to CSV files in a directory
- exportToFile() : string|false
- Export data to file
- getTableList() : array<string|int, mixed>
- Get list of tables to export
- buildInsertStatements() : string
- Build INSERT statements for table data
- escapeString() : string
- Escape string for SQL
- exportTableToCsv() : bool
- Export a single table to CSV file
- exportToCsv() : string
- Export data to CSV format Note: CSV format exports each table to a separate file
- exportToJson() : string
- Export data to JSON format
- exportToSql() : string
- Export data to SQL format
- exportToYaml() : string
- Export data to YAML format
- fetchAllAssoc() : array<string|int, mixed>
- Fetch all rows ensuring only associative keys (no numeric indices)
- formatBooleanLiteral() : string
- Format boolean value as adapter-appropriate SQL literal
- formatCsvLine() : string
- Format array as CSV line
- getTableCreateStatement() : string
- Get CREATE TABLE statement for a table
- getTableData() : array<string|int, mixed>
- Get data from a table
- getTableDataWithPDO() : array<string|int, mixed>
- Get table data using PDO prepared statements
- getTableRowCount() : int
- Get row count for a table
- getTables() : array<string|int, mixed>
- Get list of all tables in database
- hasLeadingZeros() : bool
- Check if a value has leading zeros that would be lost if treated as numeric
- parseSimpleWhereClause() : array<string|int, mixed>
- Parse a simple WHERE clause for parameterization
- quoteIdentifier() : string
- Quote a database identifier (table or column name)
- shouldUseStreaming() : bool
- Check if streaming export should be used
- streamCsvTable() : void
- Stream CSV table export
- streamExportTable() : void
- Stream export a table to file handle
- streamExportToFile() : bool
- Stream export to file for large datasets
- streamSqlTable() : void
- Stream SQL table export
- writeCsvToHandle() : int|false
- Write CSV row to a file handle (handles both compressed and uncompressed)
- writeStreamFooter() : void
- Write stream footer
- writeStreamHeader() : void
- Write stream header
- writeStreamTableSeparator() : void
- Write stream table separator
- writeToHandle() : int|false
- Write data to a file handle (handles both compressed and uncompressed)
Constants
FORMAT_CSV
public
mixed
FORMAT_CSV
= 'csv'
FORMAT_JSON
public
mixed
FORMAT_JSON
= 'json'
FORMAT_SQL
public
mixed
FORMAT_SQL
= 'sql'
FORMAT_YAML
public
mixed
FORMAT_YAML
= 'yaml'
Properties
$_Adapter
private
AdapterInterface
$_Adapter
$_AdapterType
private
string
$_AdapterType
$_MigrationTable
private
string
$_MigrationTable
$_Options
private
array<string|int, mixed>
$_Options
$fs
private
IFileSystem
$fs
Methods
__construct()
public
__construct(Config $PhinxConfig, string $Environment[, string $MigrationTable = 'phinx_log' ][, array<string|int, mixed> $Options = [] ][, IFileSystem|null $fs = null ]) : mixed
Parameters
- $PhinxConfig : Config
-
Phinx configuration
- $Environment : string
-
Environment name
- $MigrationTable : string = 'phinx_log'
-
Migration tracking table name
- $Options : array<string|int, mixed> = []
-
Export options
- $fs : IFileSystem|null = null
-
File system implementation (null = use real file system)
__destruct()
Destructor - ensure adapter is disconnected
public
__destruct() : mixed
disconnect()
Disconnect from database
public
disconnect() : void
export()
Export data to string
public
export() : string
Return values
string —Exported data
exportCsvToDirectory()
Export all tables to CSV files in a directory
public
exportCsvToDirectory(string $DirectoryPath) : array<string|int, mixed>
Parameters
- $DirectoryPath : string
-
Directory to export CSV files to
Return values
array<string|int, mixed> —List of exported files
exportToFile()
Export data to file
public
exportToFile(string $FilePath) : string|false
Parameters
- $FilePath : string
-
Path to output file
Return values
string|false —Actual file path written or false on failure
getTableList()
Get list of tables to export
public
getTableList() : array<string|int, mixed>
Return values
array<string|int, mixed>buildInsertStatements()
Build INSERT statements for table data
private
buildInsertStatements(string $table, array<string|int, mixed> $data) : string
Parameters
- $table : string
-
Table name
- $data : array<string|int, mixed>
-
Table data
Return values
string —INSERT statements
escapeString()
Escape string for SQL
private
escapeString(string $value) : string
Parameters
- $value : string
-
Value to escape
Tags
Return values
string —Escaped value
exportTableToCsv()
Export a single table to CSV file
private
exportTableToCsv(string $table, string $filePath) : bool
Parameters
- $table : string
-
Table name
- $filePath : string
-
Output file path
Return values
bool —Success status
exportToCsv()
Export data to CSV format Note: CSV format exports each table to a separate file
private
exportToCsv(array<string|int, mixed> $tables) : string
Parameters
- $tables : array<string|int, mixed>
-
List of tables
Return values
string —CSV metadata (actual data needs file output)
exportToJson()
Export data to JSON format
private
exportToJson(array<string|int, mixed> $tables) : string
Parameters
- $tables : array<string|int, mixed>
-
List of tables
Return values
string —JSON data
exportToSql()
Export data to SQL format
private
exportToSql(array<string|int, mixed> $tables) : string
Parameters
- $tables : array<string|int, mixed>
-
List of tables
Return values
string —SQL dump
exportToYaml()
Export data to YAML format
private
exportToYaml(array<string|int, mixed> $tables) : string
Parameters
- $tables : array<string|int, mixed>
-
List of tables
Return values
string —YAML data
fetchAllAssoc()
Fetch all rows ensuring only associative keys (no numeric indices)
private
fetchAllAssoc(string $sql) : array<string|int, mixed>
Phinx adapters return PDO::FETCH_BOTH by default, which includes both numeric (0,1,2...) and associative (column name) keys. This causes issues during import where numeric keys are treated as columns.
Parameters
- $sql : string
-
SQL query
Return values
array<string|int, mixed> —Rows with only associative keys
formatBooleanLiteral()
Format boolean value as adapter-appropriate SQL literal
private
formatBooleanLiteral(bool $value) : string
PostgreSQL requires TRUE/FALSE literals for boolean columns. Other databases accept 1/0 for booleans.
Parameters
- $value : bool
-
Boolean value to format
Return values
string —SQL literal representation
formatCsvLine()
Format array as CSV line
private
formatCsvLine(array<string|int, mixed> $row) : string
Parameters
- $row : array<string|int, mixed>
-
Row data
Return values
string —CSV formatted line
getTableCreateStatement()
Get CREATE TABLE statement for a table
private
getTableCreateStatement(string $table) : string
Parameters
- $table : string
-
Table name
Return values
string —CREATE TABLE statement
getTableData()
Get data from a table
private
getTableData(string $table) : array<string|int, mixed>
Parameters
- $table : string
-
Table name
Return values
array<string|int, mixed> —Table data
getTableDataWithPDO()
Get table data using PDO prepared statements
private
getTableDataWithPDO(PDO $pdo, string $table, string $whereClause) : array<string|int, mixed>
Parameters
- $pdo : PDO
-
PDO connection
- $table : string
-
Table name
- $whereClause : string
-
WHERE clause
Return values
array<string|int, mixed> —Table data
getTableRowCount()
Get row count for a table
private
getTableRowCount(string $table) : int
Parameters
- $table : string
-
Table name
Return values
int —Row count
getTables()
Get list of all tables in database
private
getTables() : array<string|int, mixed>
Return values
array<string|int, mixed>hasLeadingZeros()
Check if a value has leading zeros that would be lost if treated as numeric
private
hasLeadingZeros(mixed $value) : bool
Parameters
- $value : mixed
-
Value to check
Return values
bool —True if the value has leading zeros that need preservation
parseSimpleWhereClause()
Parse a simple WHERE clause for parameterization
private
parseSimpleWhereClause(string $whereClause) : array<string|int, mixed>
Converts "status = 'active' AND type = 'user'" to parameterized SQL Note: This is a basic implementation - production should use full SQL parser
Parameters
- $whereClause : string
-
WHERE clause to parse
Return values
array<string|int, mixed> —Array with 'sql' and 'bindings' keys
quoteIdentifier()
Quote a database identifier (table or column name)
private
quoteIdentifier(string $identifier) : string
Parameters
- $identifier : string
-
The identifier to quote
Return values
string —The properly quoted identifier for the current adapter
shouldUseStreaming()
Check if streaming export should be used
private
shouldUseStreaming() : bool
Return values
boolstreamCsvTable()
Stream CSV table export
private
streamCsvTable(resource $handle, string $table) : void
Parameters
- $handle : resource
-
File handle
- $table : string
-
Table name
streamExportTable()
Stream export a table to file handle
private
streamExportTable(resource $handle, string $table) : void
Parameters
- $handle : resource
-
File handle
- $table : string
-
Table name
streamExportToFile()
Stream export to file for large datasets
private
streamExportToFile(string $FilePath) : bool
Parameters
- $FilePath : string
-
Path to output file (may have .gz extension if compressed)
Return values
bool —Success status
streamSqlTable()
Stream SQL table export
private
streamSqlTable(resource $handle, string $table) : void
Parameters
- $handle : resource
-
File handle
- $table : string
-
Table name
writeCsvToHandle()
Write CSV row to a file handle (handles both compressed and uncompressed)
private
writeCsvToHandle(resource $handle, array<string|int, mixed> $row) : int|false
Parameters
- $handle : resource
-
File handle (regular or gzip)
- $row : array<string|int, mixed>
-
Row data to write as CSV
Return values
int|false —Number of bytes written or false on error
writeStreamFooter()
Write stream footer
private
writeStreamFooter(resource $handle) : void
Parameters
- $handle : resource
-
File handle
writeStreamHeader()
Write stream header
private
writeStreamHeader(resource $handle) : void
Parameters
- $handle : resource
-
File handle
writeStreamTableSeparator()
Write stream table separator
private
writeStreamTableSeparator(resource $handle) : void
Parameters
- $handle : resource
-
File handle
writeToHandle()
Write data to a file handle (handles both compressed and uncompressed)
private
writeToHandle(resource $handle, string $data) : int|false
Parameters
- $handle : resource
-
File handle (regular or gzip)
- $data : string
-
Data to write
Return values
int|false —Number of bytes written or false on error