Neuron-PHP

DataImporter
in package

Imports database data from various formats Supports SQL, JSON, CSV, and YAML input formats

Table of Contents

Constants

CONFLICT_APPEND  = 'append'
CONFLICT_REPLACE  = 'replace'
CONFLICT_SKIP  = 'skip'
FORMAT_CSV  = 'csv'
FORMAT_JSON  = 'json'
FORMAT_SQL  = 'sql'
FORMAT_YAML  = 'yaml'

Properties

$_Adapter  : AdapterInterface
$_AdapterType  : string
$_Errors  : array<string|int, mixed>
$_MigrationTable  : string
$_Options  : array<string|int, mixed>
$_RowsImported  : int
$_TablesImported  : int
$_Warnings  : array<string|int, mixed>
$fs  : IFileSystem

Methods

__construct()  : mixed
__destruct()  : mixed
Destructor - ensure adapter is disconnected
clearAllData()  : bool
Clear all data from database (dangerous!)
disconnect()  : void
Disconnect from database
getErrors()  : array<string|int, mixed>
Get import errors
getStatistics()  : array<string|int, mixed>
Get import statistics
getTableRowCounts()  : array<string|int, mixed>
Get table row counts for all tables in database
getWarnings()  : array<string|int, mixed>
Get import warnings
import()  : bool
Import data from string
importFromCsvDirectory()  : bool
Import all CSV files from a directory
importFromFile()  : bool
Import data from file
verifyImport()  : array<string|int, mixed>
Verify import by checking row counts
detectFormat()  : string
Detect format from file extension or content
disableForeignKeyChecks()  : void
Disable foreign key checks
enableForeignKeyChecks()  : void
Enable foreign key checks
escapeString()  : string
Escape string for SQL
estimateRowsFromInsert()  : int
Estimate number of rows from INSERT statement
executeWithTransactionManagement()  : bool
Execute import callback with transaction and foreign key management
filterTables()  : array<string|int, mixed>
Filter tables based on options
formatBooleanLiteral()  : string
Format boolean value as adapter-appropriate SQL literal
getAllTables()  : 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
importCsvFile()  : void
Import a single CSV file
importFromJson()  : bool
Import data from JSON format
importFromSql()  : bool
Import data from SQL format
importFromYaml()  : bool
Import data from YAML format
importStructuredData()  : bool
Import structured data (from JSON/YAML)
importTableData()  : void
Import table data
insertBatch()  : void
Insert a batch of rows
prepareTableForImport()  : bool
Prepare table for import based on conflict mode
quoteIdentifier()  : string
Quote a database identifier (table or column name)
shouldProcessStatement()  : bool
Check if a statement should be processed based on table filters
shouldProcessTable()  : bool
Check if a table should be processed
splitSqlStatements()  : array<string|int, mixed>
Split SQL string into individual statements

Constants

CONFLICT_REPLACE

public mixed CONFLICT_REPLACE = 'replace'

Properties

$_Warnings

private array<string|int, mixed> $_Warnings = []

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> = []

Import options

$fs : IFileSystem|null = null

File system implementation (null = use real file system)

__destruct()

Destructor - ensure adapter is disconnected

public __destruct() : mixed

clearAllData()

Clear all data from database (dangerous!)

public clearAllData([bool $includeMigrationTable = false ]) : bool
Parameters
$includeMigrationTable : bool = false

Whether to clear migration table too

Return values
bool

Success status

disconnect()

Disconnect from database

public disconnect() : void

getErrors()

Get import errors

public getErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>

getStatistics()

Get import statistics

public getStatistics() : array<string|int, mixed>
Return values
array<string|int, mixed>

getTableRowCounts()

Get table row counts for all tables in database

public getTableRowCounts() : array<string|int, mixed>
Return values
array<string|int, mixed>

Table => row count mapping

getWarnings()

Get import warnings

public getWarnings() : array<string|int, mixed>
Return values
array<string|int, mixed>

import()

Import data from string

public import(string $data) : bool
Parameters
$data : string

Data to import

Return values
bool

Success status

importFromCsvDirectory()

Import all CSV files from a directory

public importFromCsvDirectory(string $DirectoryPath) : bool
Parameters
$DirectoryPath : string

Directory containing CSV files

Return values
bool

Success status

importFromFile()

Import data from file

public importFromFile(string $FilePath) : bool
Parameters
$FilePath : string

Path to input file

Return values
bool

Success status

verifyImport()

Verify import by checking row counts

public verifyImport(array<string|int, mixed> $expectedCounts) : array<string|int, mixed>
Parameters
$expectedCounts : array<string|int, mixed>

Array of table => expected row count

Return values
array<string|int, mixed>

Verification results

detectFormat()

Detect format from file extension or content

private detectFormat(string $filePath, string $content) : string
Parameters
$filePath : string

File path

$content : string

File content

Return values
string

Detected format

disableForeignKeyChecks()

Disable foreign key checks

private disableForeignKeyChecks() : void

enableForeignKeyChecks()

Enable foreign key checks

private enableForeignKeyChecks() : void

escapeString()

Escape string for SQL

private escapeString(string $value) : string
Parameters
$value : string

Value to escape

Tags
throws
RuntimeException

if safe escaping is not available

Return values
string

Escaped value (without surrounding quotes)

estimateRowsFromInsert()

Estimate number of rows from INSERT statement

private estimateRowsFromInsert(string $statement) : int
Parameters
$statement : string

INSERT statement

Return values
int

Estimated row count

executeWithTransactionManagement()

Execute import callback with transaction and foreign key management

private executeWithTransactionManagement(callable $importCallback) : bool

This helper centralizes the common pattern of:

  1. Disabling foreign keys (if requested)
  2. Clearing tables (if requested)
  3. Beginning transaction (if requested)
  4. Executing the import callback
  5. Re-enabling foreign keys
  6. Committing/rolling back transaction
  7. Handling errors with rollback and FK re-enable in finally block
Parameters
$importCallback : callable

The import operation to execute

Return values
bool

Success status

filterTables()

Filter tables based on options

private filterTables(array<string|int, mixed> $tables) : array<string|int, mixed>
Parameters
$tables : array<string|int, mixed>

List of table names

Return values
array<string|int, mixed>

Filtered list

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

getAllTables()

Get list of all tables in database

private getAllTables() : 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

importCsvFile()

Import a single CSV file

private importCsvFile(string $filePath, string $tableName) : void
Parameters
$filePath : string

CSV file path

$tableName : string

Target table name

importFromJson()

Import data from JSON format

private importFromJson(string $json) : bool
Parameters
$json : string

JSON data

Return values
bool

Success status

importFromSql()

Import data from SQL format

private importFromSql(string $sql) : bool
Parameters
$sql : string

SQL data

Return values
bool

Success status

importFromYaml()

Import data from YAML format

private importFromYaml(string $yaml) : bool
Parameters
$yaml : string

YAML data

Return values
bool

Success status

importStructuredData()

Import structured data (from JSON/YAML)

private importStructuredData(array<string|int, mixed> $data) : bool
Parameters
$data : array<string|int, mixed>

Structured data with table names as keys

Return values
bool

Success status

importTableData()

Import table data

private importTableData(string $table, array<string|int, mixed> $rows) : void
Parameters
$table : string

Table name

$rows : array<string|int, mixed>

Data rows

insertBatch()

Insert a batch of rows

private insertBatch(string $table, array<string|int, mixed> $rows) : void
Parameters
$table : string

Table name

$rows : array<string|int, mixed>

Rows to insert

prepareTableForImport()

Prepare table for import based on conflict mode

private prepareTableForImport(string $table) : bool
Parameters
$table : string

Table name

Return values
bool

Whether to proceed with import

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

shouldProcessStatement()

Check if a statement should be processed based on table filters

private shouldProcessStatement(string $statement) : bool
Parameters
$statement : string

SQL statement

Return values
bool

shouldProcessTable()

Check if a table should be processed

private shouldProcessTable(string $tableName) : bool
Parameters
$tableName : string

Table name

Return values
bool

splitSqlStatements()

Split SQL string into individual statements

private splitSqlStatements(string $sql) : array<string|int, mixed>
Parameters
$sql : string

SQL string

Return values
array<string|int, mixed>

Array of SQL statements


        
On this page

Search results