Load data from CSV, XML, and JSON files into your database.
$db->loadCsv('users', 'users.csv', [
'name', 'email', 'age'
]);$db->loadCsv('users', 'users.csv', [
'columns' => ['name', 'email', 'age'],
'delimiter' => ',',
'enclosure' => '"',
'escape' => '\\',
'header' => true,
'skip' => 1
]);use tommyknocker\pdodb\helpers\Db;
// Process in batches
$handler = function($row) use ($db) {
$db->find()
->table('users')
->insert([
'name' => $row[0],
'email' => $row[1],
'created_at' => Db::now()
]);
};
$db->loadCsv('users', 'large_file.csv', [
'handler' => $handler,
'batch_size' => 1000
]);$db->loadXml('products', 'products.xml', [
'name' => 'title',
'price' => 'cost',
'stock' => 'quantity'
]);$db->loadXml('products', 'products.xml', [
'name' => 'title',
'price' => 'cost',
'xml_namespace' => 'http://example.com/products'
]);$db->loadXml('products', 'products.xml', [
'mapping' => [
'name' => '//product/title',
'price' => '//product/price',
'description' => '//product/description'
]
]);Load data from a JSON array format:
$db->find()
->table('products')
->loadJson('products.json');Load data from a newline-delimited JSON format:
$db->find()
->table('products')
->loadJson('products.ndjson', [
'format' => 'lines',
]);$db->find()
->table('products')
->loadJson('products.json', [
'columns' => ['name', 'price', 'stock'],
'batchSize' => 1000,
'format' => 'auto', // 'auto', 'array', 'lines'
]);PDOdb supports two JSON formats:
Array format (default):
[
{"name": "Laptop", "price": 1299.99, "stock": 15},
{"name": "Mouse", "price": 29.99, "stock": 150}
]NDJSON format (newline-delimited):
{"name": "Laptop", "price": 1299.99, "stock": 15}
{"name": "Mouse", "price": 29.99, "stock": 150}The NDJSON format is useful for very large files as it allows streaming without loading the entire file into memory.
JSON values containing nested objects or arrays are automatically encoded:
$db->find()
->table('products')
->loadJson('products.json');Where products.json contains:
[
{
"name": "Laptop",
"specs": {"ram": "16GB", "cpu": "Intel i7"},
"tags": ["gaming", "professional"]
}
]The nested data will be stored as JSON-encoded strings in the database.
- File Loading - CSV loading examples
- File Loading - XML loading examples
- File Loading - JSON loading examples
- Bulk Operations - Multi-row operations
- Batch Processing - Large datasets
- Troubleshooting - Common issues